I am using the GAPI API to access Google Analytics rather than do it myself directly (I know slightly lazy...). I've had a look at the class file but I can't see any built-in function for checking sampling. I was wondering if anyone who has used it has found a way to check if the results being returned are being sampled.
This is the code I am using.
$this->load->config('gapi');
$params = array('client_email' => $this->config->item('account_email'),
'key_file' => $this->config->item('p12_key'));
$this->load->library('gapi', $params);
$this->gapi->requestReportData(
$this->config->item('ga_profile_id'), //reportID
array('date', 'transactionId', 'campaign'), //Dimensions
array('transactionRevenue'), //Metrics
'', //Sort Metric
'medium==email', //Filters
date('Y-m-01'), //Start Date
date('Y-m-d'), //End Date
1,
500
);
$results = $this->gapi->getResults();
My plan is to run the report for a given date range, check to see if the data is sampled and if true, split the query into small parts to get around it.
The v3 API has 2 sampling-related response fields:
Sample Size: number of data entries used
Sample Space: number of data entries available
So if you do Sample Size / Sample Space you have your sampling ratio.
The v4 API has the same but named differently:
samplesReadCounts: number of data entries used
samplingSpaceSizes: number of data entries available
So if you do samplesReadCounts / samplingSpaceSizes you have your sampling ratio.
If gapi doesn't expose these fields in the response, then you should change API client (eg use the official client)
Related
I'm using the PHP V2 API. I requested full access (scope: https://www.googleapis.com/auth/drive). I also tried adding all of the different scopes to no avail.
I'm fully able to retrieve all files and list them but the thumbnail link is always null. Same with 'hasThumbnail'.
I tried the API explorer on https://developers.google.com/drive/v2/reference/files/get#examples and it shows me the thumbnail links correctly.
The relevant code can be boiled down to this:
$drive = new Google_Service_Drive($this->client);;
$files = $drive->files->listFiles($parameters)->files;
This is the response from the API explorer.
The response from my code (for the same ID) is:
Found the solution. It took 5 hours to run into this such simple solution.
Not many fields show by default, you must specify which fields you want populated.
The modified basic query now is:
$this->drive->files->listFiles([
'fields' => 'nextPageToken, files(thumbnailLink, webViewLink)'
])->files;
I would like to use the Google Sheets API Batch Update because the calls to update individual cells are extremely slow. The problem is that cells that have no data do not come back as part of a cell feed, so I do not have the necessary edit URL to include those cells in the Batch Update. I have seen reference to a "return-empty" parameter in the Google .NET API which will supposedly return all cells, even empty cells, in a cell feed, but I cannot find any reference to that for the "Protocol" API I am using in PHP (here is an example of a reference to the "return-empty" parameter: Writing to an empty cell in Google Spreadsheets). Does anyone know how to get the cell feed request to return all cells (including empty cells)? Or am I doomed to using the abysmally slow cell update requests?
Quick Hack
If you're using the same API I am, then in googlespraedsheet/api.php at line 152 after
$cellRangeCriteriaQuerystringList = [];
You can add:
$cellRangeCriteriaQuerystringList[] = "return-empty=true";
Better Way
There's a better way, which I have added in my own fork. I created a pull request.
If you work with my fork (or if the pull request is accepted), then you can just add 'returnEmpty' => true to your $cellRange. For example:
$cellList = $spreadsheetAPI->getWorksheetCellList(
$spreadsheetKey,
$worksheetID,
['rowStart' => $rowStart,
'rowEnd' => $rowEnd,
'returnEmpty' => true]);
Using Foursquare API it returns me the following JSON feed
{"meta":{"code":200},"notifications":[{"type":"notificationTray","item":{"unreadCount":1}},{"type":"message","item":{"message":"OK! We've got you # Place XXX. You've been here 2 times."}},{"type":"insights","item":{"insights":{"count":4,"items":[{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First of your friends to check into Place XXX!","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First of your friends to check into Place XXX!","points":5}},{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First time at Place XXX.","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First time at Place XXX.","points":5}},{"type":"pointsReward","image":"https://foursquare.com/img/points/category-outdoor.png","title":"First Plaza check-in of your friends!","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/category-outdoor_","sizes":[44,60,120],"name":".png"},"message":"First Plaza check-in of your friends!","points":6}},{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First of your friends to check in here.","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First of your friends to check in here.","points":2}}]}}},{"type":"leaderboard","item":{"total":18,"scores":[{"icon":"https://foursquare.com/img/points/discovery-venue3.png","message":"First of your friends to check into...
What I would like to do is to display the message "OK! We've got you # Place XXX. You've been here 2 times." which is embedded in the second "type" tag
I'm used to do get this kind of value using a foreach function but with this one I cannot get it.
I need help from an expert, thanks for your participation.
Code Im trying to use without success:
$data = json_decode($response, true); // return array not object
foreach($data['notifications']['type'][0] as $item) {
echo $item['item']['message'];
}
notifications is an array, so you can't hash into it with 'type' like you are trying to do. You should iterate through $data['notifications'] until you find an object with a "type" property of "message". Then dig into that object's "item" and get its "message". For more details about the notifications field in a response, see https://developer.foursquare.com/docs/responses/notifications.
In the sample JSON you provided, to directly access the content you want, you could use $data['notifications'][1]['item']['message']. The 1 may change depending on what API version you're passing in, but should be relatively stable as long as you keep using the same version.
I am currently working with the Youtube Analytics API,
So far i have been able to pull all the data that youtube has to offer except the gender/ageGroup dimensions the query fails everytime,
The docs point to examples to playbacklocation and not the demographic it's self.
I am using the PHP Client Library,
== PHP ==
$analytics_gender = new Google_YouTubeAnalyticsService($client);
$optparam = array('dimensions' => 'gender');
$metrics= "views";
$analytics_demo_gender = $analytics_gender->reports->query("channel==".$channelId, "2012-08-14", "2013-05-30", $metrics, $optparam);
When i run this query i get an error (400) The query is not supported. even though it works just fine for all the other metrics and dimensions.
The gender dimension can only be used with the metric viewerPercentage (and optionally with a country and/or video filter and with an additional ageGroup dimension, if you'd like). You can search the relevant documentation for "gender" to see the exact specifications.
Here's an example of a working report in the API Explorer. Authenticate, and replace CHANNEL_ID with the id of your channel.
It's simple to do this, please check below code snippet to find demographic and gender statics:
request.get({
url:'https://www.googleapis.com/youtube/analytics/v1/reports?key={Google Api Key}&ids=channel=={channelId}&dimensions=country&metrics=views&end-date={endDate}&start-date={startDate}',
json:true,
timeout: 10000,
headers:{'Authorization':'Bearer '+accessToken}},
function (err,r,result) {
console.log(result)
});
If you need to find gender info below code snippet can be used:
request.get({
url:'https://www.googleapis.com/youtube/analytics/v1/reports?key={Google Api Key}&ids=channel=={channelId}&dimensions=gender&metrics=viewerPercentage&end-date={endDate}&start-date={startDate}',
json:true,
timeout: 10000,
headers:{'Authorization':'Bearer '+accessToken}},
function (err,r,result) {
console.log(result)
});
If you need to find gender info along with ageGroup below code snippet can be used
request.get({
url:'https://www.googleapis.com/youtube/analytics/v1/reports?key={Google Api Key}&ids=channel=={channelId}&dimensions=gender,ageGroup&metrics=viewerPercentage&end-date={endDate}&start-date={startDate}',
json:true,
timeout: 10000,
headers:{'Authorization':'Bearer '+accessToken}},
function (err,r,result) {
console.log(result)
});
I am using the Google Analytics PHP class to get data from Google Analytics.
http://code.google.com/p/gapi-google-analytics-php-interface/wiki/GAPIDocumentation
I would like to get a report of "Bounce Rate" For "Top Contnet".
The thing is I am not familiar with the terminology.
When I am trying to get a "content" report or "topcontent" or "top_content" it says that there in no such metric. I simply don't know the right expressions.
Does anyone know where can I find a list of all expressions? metrics & dimensions?
Thanks.
Top content isn't a metric, it's just a list of the pages on your site with the highest number of page views.
The metric you're looking for is 'entranceBounceRate' and the dimension is 'pagePath'. You want to get the bounce rate for the top X most visited pages on your site, so you'll want to limit your results and sort the results by '-pageviews' (pageviews descending).
If you want to get the bounce rate for the top 10 most viewed pages on your site, your query should look like this:
$ga = new gapi('email#yourdomain.com','password');
$ga->requestReportData(145141242,array('pagePath'),array('entranceBounceRate','pageviews'),array('-visits'),null,null,null,10);
The Google Analytics Export API has a data feed query explorer that should help you out considerably when using GAPI:
http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
Also, here's a list of all available dimensions and metrics you can pull from the API:
http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html
Definitely read over the GAPI documentation:
http://code.google.com/p/gapi-google-analytics-php-interface/wiki/GAPIDocumentation
If you would like to get the global Bounce Rate for the last 30days (by default), here is how. Very simple once you know it.
//Check Bounce Rate for the last 30 days
$ga = new gapi(ga_email, ga_password);
$ga->requestReportData(145141242, NULL ,array('bounces', 'visits'));
$data = round(($ga->getBounces() / $ga->getVisits()) * 100) . "%";
Note that the GAPI has a bug, they mention the dimension parameter is optional (2nd parameter) but it's not. You have to open the gapi.class.php file and patch line 128 with this:
//Patch bug to make 2nd parameter optional
if( !empty($dimensions) ) {
$parameters['dimensions'] = 'ga:'.$dimensions;
} else {
$parameters['dimensions'] = '';
}