问题
I've been researching this for a little while now, and from what I gather, it has something to do with samplingLevel.
The issue I'm gathering from most other stackoverflow questions is that unless I have a Premium Account, the data will always come back as sampled.
It is worth asking, is there anyway to can alter my Google API query so the data is a little more accurate?
My query code:
$profiles = $analytics->management_profiles
->listManagementProfiles('myID', '~all');
foreach ($profiles->getItems() as $profile) {
$IDvalue = $profile->getId();
array_push($profilesArray, $IDvalue);
}
foreach ($profilesArray as $p) {
$results = $analytics->data_ga->get(
'ga:' . $p,
'7daysAgo',
'today',
'ga:sessions');
$profileName = $results->getProfileInfo()->getProfileName();
$rows = $results->getRows();
$sessions = $rows[0][0];
print "Profile Name: $profileName";
echo "<br>";
print "Total Sessions: $sessions";
echo "<br><br>";
}
I tried changing my get()
to:
$results = $analytics->data_ga->get(
'ga:' . $p,
'7daysAgo',
'today',
'ga:sessions',
'samplingLevel:HIGHER_PRECISION');
I've also tried:
$results = $analytics->data_ga->get(
'ga:' . $p,
'7daysAgo',
'today',
'ga:sessions',
'ga:samplingLevel==HIGHER_PRECISION');
But the query breaks and says the id's are missing along with multiple other errors. I realize that I'm probably not making the query right, but anyone who can point out what the proper way to write my query would be helping out greatly. That and is this approach possible? Or will I need a Premium Account to accomplish what I'm trying to do?
回答1:
Sampling
Sampling tends to occur when you have high number of sessions or events for a given time period. Options to handle sampling:
- Shorten the date range.
- Reduce number of dimensions.
- Increase the
samplingLevel
.
Take the guess work out things and verify if your results containSampledData
by checking the response for the field containsSampledData. Also in your query you are requesting today's data, in the UI by default they show you up to Yesterday's data. Today's data is still coming in, so depending on when you query the API you will get a different answer for the number of sessions.
API errors:
There are some issues with your code. I would suggest looking at some of the examples in the docs and look at the reference docs to understand how the API is structured. For example you need to pass in the optional parameters as an array:
foreach ($profilesArray as $p) {
$optParams = array(
'dimensions' => 'ga:source,ga:keyword',
'sort' => '-ga:sessions,ga:source',
'filters' => 'ga:medium==organic',
'max-results' => '25',
'samplingLevel' => 'HIGHER_PRECISION');
$results = $analytics->data_ga->get(
'ga:' + $p,
'7daysAgo',
'today',
'ga:sessions',
$optParams);
...
// Do something with the $results.
}
Words of warning, the API is subject to Limits and Quotas, so if you have more than 10 Views (profiles) your API will return a ratelimiting error for querying too quickly. It is good practice to implement rate limiting and exponential backoff.
Migrate to Analytics Reporting API V4
We all like to have the shiny new toy. Go ahead think about migrating to Analytics Reporting API V4. You've already done the hard work of figuring out OAuth, And they made available a great Migration Guide
StackOverflow Advice
StackOverflow is a great place to get help with your implementations, and you did great job of including your code (you'd be surprised how many people don't). I would also recommend including your error responses, stacktraces as well as the resources you've seen online.
来源:https://stackoverflow.com/questions/38791878/google-analytics-api-why-is-the-api-data-different-than-whats-being-seen-on-th