Laravel and google analytics

Google analytics do what we need many time like recording the user interactions, page views, and the trend user follow while exploring the site.

laravel and google analytics
Laravel + Google Analytics

Setup spatie/laravel-analytics

You can read how to setup package here. It is quite easy but if you find any problem just ask in comment.

Extending the spatie/laravel-analytics

You are reading this means that you have successfully installed the laravel-anlytics on your site and might using the page views.
You notice that you can get treading item by using the builtin function but what if you want to display the page-views for the each of posts on their page or catalog like
We can create our own function to fulfill  this.
By the way, i am very thankful to the creator of this package who has created the package in the way any one can create their custom function to run the custom query on google analytics.

So to create custom query function just follow me :
location of analytics.php
File to be edited

  • Go to vendors folder of you laravel package
  • search for spatie
  • open src folder
  • and you will get file list as show in the image

Now this is the time to under stand how the built in function works
Functions to be understand
How query performed and response returned

So we will take fetchMostVisitedPages function as example

So what is does is

  • First it calls the function performQuery with the parameters time period, metrics and other
  1. Time period is the parameter which decides data of which time span is used. For example you need trend of last week you select time span of last week only.
  2. Second is metrics, defined as "A metric is a number which is used to measure one of the characteristics of a dimension "
  3. Last is other parameters, here you define the other parameters of query like dimensions, sort, max-results, filter etc..

  • function performQuery runs the api query behind the scene and returns data that is converted to the collection object before returning. So at the end we get collection of object instead of json data.
  • you can also modify the fields of return by changing the return inside map's callback function.

My Requitemnt : I want a query function that returns the page-view count for a every single url and than i want to show it on my site like this

what i want 

So here, The data that will be provided by laravle is

  • Slug
  • url
  • name
  • other stored data
So what i need is asking google analytics the about count of the url of this post during the time span from when it has been published

The function will look like this
function that returns the page view for a particular
url fetched from the google analytics api

So what is does is,

  • Creates a period from the date on which it created to the till now
  • I used uniquePageViews as matric because i want to count only unique page views. pageViews Does not ignore the refreshses
  • Next the thing that i added is filter, This is the thing that makes the function differnet from other 
  1. If i don't put filter than it will give result as list of page view count of all pages
  2. but i want only count for the spacific url
  3. so using the filter it will only return the result where url matches the url of my post, and because every post have unique url i will get only one result per request.
  4. I have also edited the return to only the page count. it is " return (int) $pageRow[2] ; " 

Hope you find it useful.


  1. A functional method to resolve contemporary functioning requirements, the work-flow upgrades to the Google Search Method will assist you to improve partnership in addition to resolve difficulties simpler read the full info here. Clients will now have the capacity to reveal ticket products with various members of their group inside the platform.

  2. Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have. This is helpful, nonetheless it can be crucial so that you can check out the following website:

  3. Get your own certification from EXCELR if you think that you are interested in entering the giant web of data science course in hyderabad

  4. Data science is the study of data and how it can be exploited and manipulated in order to solve business problems, enhance the user experience, and making machines more accurate. data science course in hyderabad


Post a Comment