Search code examples
google-analyticsgoogle-apigoogle-analytics-apisample-data

How to get unsampled data from Google Analytics API - even for one day?


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...)?


Solution

  • 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.