Skip to main content

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 slideshare.com
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.


Comments

  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.

    ReplyDelete
  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. https://www.pageview.com/ This is helpful, nonetheless it can be crucial so that you can check out the following website:

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete

Post a Comment

Popular posts from this blog

Google blogger Ideas panel

Google blogger Ideas  I opened by blogger today, and..   I got this.  Google blogger Ideas  A panel suggesting a topic on which I can write my next blog. It's fetching unanswered question from web according to your previous post and topics. It was something, I was really looking for, after all it takes time to finding subject on which to write next and still being in the same niche.  Awesome feature Blogger! 

Apache : setup basic auth with apache in windows

Authentication is any process by which you verify that someone is who they claim they are. Authorization is any process by which someone is allowed to be where they want to go or to have information that they want to have. I will show here how to set up basic auth on the apache with windows. Pre-requests  Windows VPS Apache server ( That's it ) ( In windows it might be difficult to setup the Apache alone. So instead use something ling xampp , wamp or laragon .) RestClient (  I personally use the postman , but you can use your preferable client)  Windows VPS provider Steps  Enable the necessary modules in the Apache Create the password file Set the auth directives in the virtual host file. Verify basic auth. Enable the  necessary   modules  in the Apache Open the httpd.conf file in the apache's conf folder. httpd.conf file Enable the necessary modules to make the basic auth working. Necessary modules  mod_auth_basic

Firebase - update a spacific fields of single element of object of array in firestore

Firebase - update a spacific fields of single element of object of array in firestore  Its actully advisable to use map instead of array when ever it is possible. But, there are cetain cases where you don't have option to do so.  For example, you are directly saving the response from some outer source without any modification and they send you an array. In this case you will have array to work with. Firestore does not support array here is why  "bad things can happen if you have multiple clients all trying to update or delete array elements at specific indexes. In the past, Cloud Firestore addressed these issues by limiting what you can do with arrays " For more details information you can refer to Kato Richardson post Best Practices: Arrays in Firebase .  Firestore document having array [ used from stackoverflow question ] Suppose you have array of object something like shown in array. Now you want to update endTime field of the object on the index [1]