2

I'm trying to obtain unsampled data from the Google Analytics API, but for some reason it always comes out sampled. Even if I select only one day and filter for one page only. This is what I've tried on Google's Query Explorer:

Sampled Data Always

What do I need to do to overcome this? Also, is there a way to see how much of the data is sampled (without having to log into the Google Analytics page...)?

TheBigDoubleA
  • 432
  • 2
  • 7
  • 26

2 Answers2

3

In your query you need to supply a sampiling level

samplingLevel=DEFAULT Optional.
  Use this parameter to set the sampling level (i.e. the number of visits used to 
  calculate the result) for a reporting query. The allowed values are consistent with
  the web interface and include: 
       •DEFAULT — Returns response with a sample size that balances speed and accuracy. 
       •FASTER — Returns a fast response with a smaller sample size. 
       •HIGHER_PRECISION — Returns a more accurate response using a large sample size, 
         but this may result in the response being slower. 

If not supplied, the DEFAULT sampling level will be used.

You haven't stated which language you are using so your going to have to check the Library for that language and figure out how to send it.


Update: trying to help with code. I haven't tested this, but my guess is you would add it to as an optional parameter. Let me know if it doesn't work and i will see if i can get it working.

$optParams = array(
    'dimensions' => 'ga:dateHour,ga:hour', 
    'filters' => 'ga:pagePath=~'.$pagelink.'*', 
    'max-results' => 1, 
    'sort' => 'ga:dateHour',
    'samplingLevel' => 'HIGHER_PRECISION' );


$results_starttime = $connect->data_ga->get( 'ga:' . $signedupuser["google id"],
$startdate_analysed, 
$enddate_analysed, 
'ga:uniquePageviews', $optParams );

Update2: Make sure you downloaded the lib from GitHub google/google-api-php-client i checked and Analytics.php from there does have code to support samplingLevel.


Update 3 Your getting the old version of the lib get the one from GitHub check link above. google-api-php-client

Status:

The beta release of the next major revision (1.0.1-beta) of the library is available, please migrate when possible. The newer version is published on GitHub. All new development, and issue tracking, will occur on Github.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Great, and where do you add it? This is my code: $results_starttime = $connect->data_ga->get( 'ga:' . $signedupuser["google id"], $startdate_analysed, $enddate_analysed, 'ga:uniquePageviews', 'samplingLevel=HIGHER_PRECISION', array( 'dimensions' => 'ga:dateHour,ga:hour', 'filters' => 'ga:pagePath=~'.$pagelink.'*', 'max-results' => 1, 'sort' => 'ga:dateHour' ) ); – TheBigDoubleA Feb 10 '14 at 18:45
  • Check edit i think its probably an optional Parameter. – Linda Lawton - DaImTo Feb 10 '14 at 19:26
  • Nope, that doesn't work. I added the optional parameter but I get this error message: Fatal error: Uncaught exception 'Google_Exception' with message '(get) unknown parameter: 'samplingLevel' – TheBigDoubleA Feb 10 '14 at 23:10
  • Where did you download the library from? https://github.com/google/google-api-php-client – Linda Lawton - DaImTo Feb 11 '14 at 07:43
  • 2
    Read the Status on the page you just linked. Get the latest version from GitHub. – Linda Lawton - DaImTo Feb 11 '14 at 12:14
1

Did you by any chance exceed the daily limits (500,000 visits) for sampling? If so, getting unsampled data is technically not possible.

Otherwise, see this answer on very similar question.

Community
  • 1
  • 1
Petr Havlik
  • 3,307
  • 1
  • 19
  • 17
  • Well, the daily visits for the entire website do exceed 500,000, however, I'm only tracking individual pages within the website, where the visits are around 30,000 a day. Is that still a problem? If yes, how can I overcome this? – TheBigDoubleA Feb 12 '14 at 11:25
  • I regularly use the API to select out millions of rows with out sampling the data. By adding samplingLevel=HIGHER_PRECISION the resulting json states that the data has not been sampled. So i'm not sure what you mean about it not being possible. – Linda Lawton - DaImTo Feb 12 '14 at 11:47
  • DaImTo - I do as well, but if you run a query that will return more than 500,000 visits; the data will be sampled unless you are lucky enough to be using the Premium version: https://support.google.com/analytics/answer/1042498?hl=en – Petr Havlik Feb 12 '14 at 23:09
  • As for the higher_precision attribute -- it's the same as if you drag the slider in Google Analytics web interface. It allows you to pick up data from to 500,000 visits. CoreReporting API has the same limits. – Petr Havlik Feb 12 '14 at 23:11