Search code examples
phpgoogle-analyticsgoogle-analytics-api

How to pull visits for certain urls from GA API or GAPI?


Basically I have a blog that I want to show the number of unique views for each post if you are logged in as an admin (I need it to calculate statistics that I can't do just looking at the regular GA admin).

So for the input it would be series of URL paths.

For example, if you're an admin looking at page 3 of the posts from a few days ago, and theres 10 posts per page, it would be 10 input paths.

Like such...

input:

'/hello-world/'
'/this-demo-post/'
'/woot/'

Then it would output something somewhat like this, where the array value is the number of unique visitors:

[0] => 1003,
[1] => 140,
[2] => 7761

At this point I would just output that in JSON on the page, and then use JS to make my calculations next to each post.

The tricky part is how to get this information from GA, as its seems to just pull in ALL of the data. Even if I limit the date to 3 days old, it still pulls all the data of way older posts that are still getting a lot of traffic. It clutters the results and may even possibly 'push out' some posts I want to see if it doesn't fit within the result scope.

I was thinking of maybe using filter regex with a lot of (this|or|that) on the 10 paths?

Here is what I have so far.

require 'gapi.class.php';

date_default_timezone_set('America/New_York');
$dimensions = array('pagePath');
$metrics    = array('uniquePageviews'); // visits
$sortMetric = null;
$filter     = null;
$startDate  = date('Y-m-d', strtotime('-2 days'));
$endDate    = date('Y-m-d');
$startIndex = 1;
$maxResults = 10000;

$ga = new gapi($gaEmail, $gaPassword);

$ga->requestReportData($profileId, $dimensions, $metrics, $sortMetric, $filter, $startDate, $endDate, $startIndex, $maxResults);

Edit In retrospect, this would all be solved with a way to sort by 'first seen' and not just by total views. Is this in GAPI?


Solution

  • Tallboy,

    this one could be tricky :-)

    Let me start with the possible issues:

    • filter character limit (128 characters)
    • keep in mind that unique visitors and unique pageviews are slightly different metrics, and both could lead to strange numbers if date ranges are used incorrectly (if you run a query on daily basis for a range of days, everyone would be "unique" every single day)

    Now to answer your question:

    The tricky part is how to get this information from GA, as its seems to just pull in ALL of the data.

    The reason for this is actually that you did not specify anything in filter, so when you query your account with pagePath dimension and uniquePageviews metric, you get the numbers for all the pages.

    But would that be an issue? Could you access the row with specific page and the associated number with that page and then use it in your report?

    If not, the only reasonable and automated way to me seems splitting your query into multiple queries, and actually running it for every single page and always use the pagePath in the filter.

    If there is couple of them, that should be OK - if hundreds/thousands, not the smartest thing probably. Also, don't forget about the limits and API quota.

    What I would suggest however, is to go with custom variables / custom dimensions and send your internal PageID with every pageview. That way, the pairing could be much easier. Simply use the PostID to find the right number and display it.

    Hope this helps.