Saturday , February 23 2019
Home / PHP / Retrieve Google Analytics Visits and PageViews with PHP

Retrieve Google Analytics Visits and PageViews with PHP

Google Analytics is an outstanding website analytics tool that gives you way more information about your website than you probably need. Better to get more than you want than not enough, right? Anyways I check my website statistics more often than I should and it ends up taking a few minutes to get logged in, select the right site, select the current day, etc. I found a great Google Analytics PHP API that allows me to get just the statistics I’m looking for.

The PHP Library

The PHP class I found, analytics, can be downloaded at The site also gives a few solid examples.


As you’d expect, we first grab the class and immediately sign providing your credentials and website profile (you can just use your domain). Once authenticated we set a date range and retrieve our visitors and pageviews.

The Sample PHP Results

The above code retrieved visits and pageviews for the current month. What’s returned is an array that looks as follows:
[01] => 6539
[02] => 6677
[03] => 6160
[04] => 5563
[05] => 2964
[06] => 2973
[07] => 5080
[08] => 6078
[09] => 5927
[10] => 6177

A very simple array numbered by day. You could do anything you wanted with the array — create averages, peaks, lows, etc.

About admin

Check Also

Resize Images in php

Creating thumbnails of the images is required many a times, this code will be useful ...

Leave a Reply