Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you retrieve only Total Unique Visitors from Google Analytics API with PHP

I've been looking at this tutorial http://www.codediesel.com/php/reading-google-analytics-data-from-php/ but am struggling as I only want to retrieve the total number of unique visitors which is a metric.

It appears that you have to use a 'dimension' to get the 'metric' when using their API.

Any ideas on how I can avoid using a dimension as all I want is the total.

Thanks

like image 663
aphextwig Avatar asked Dec 16 '11 23:12

aphextwig


2 Answers

Dimension is not mandatory.

Use:

Metric: ga:visitors and do mention the start-date and end-date to get the data in the desired range, which is required. You will get the desired total.( P.S. The calculation of ga:visitors has been changed to return the number of unique visitors across the date range )

like image 113
samridhi Avatar answered Sep 30 '22 01:09

samridhi


$analytics = new Google_Service_Analytics($client);

$profileId = 'UA-00000000-1';

$yesterday=$startDate=$endDate=date("Y-m-d", time() - 60 * 60 * 24 );

$metrics = 'sessions';

$results = $analytics->data_ga->get('ga:'.$profileId, $startDate, $endDate, 'ga:'.$metrics);

Requires - https://github.com/google/google-api-php-client

like image 43
RafaSashi Avatar answered Sep 30 '22 00:09

RafaSashi