Skip to content

Instantly share code, notes, and snippets.

@itayw
Forked from anddimario/gist:8559849
Created March 25, 2014 18:03
Show Gist options
  • Save itayw/9767557 to your computer and use it in GitHub Desktop.
Save itayw/9767557 to your computer and use it in GitHub Desktop.
googleapis
.discover('analytics', 'v3')
.execute(function(err, client) {
if (err) {
utils.applog('error', {from: "lib/fetch/analytics.js", value: err, reference: "getAnalytics", resource: user_id});
return;
}
// Array that contains all request
var requests_list = [
{metrics: "ga:visits,ga:newVisits,ga:percentNewVisits,ga:newVisits,ga:pageviews,ga:visitBounceRate,ga:pageviewsPerVisit,ga:avgTimeOnSite"},
{metrics: "ga:visits", dimensions: "ga:visitorType,ga:medium"},
{metrics: "ga:visits, ga:pageviews", dimensions: "ga:visitCount,ga:medium"},
{metrics: "ga:goalCompletionsAll", dimensions: "ga:medium"},
{metrics: "ga:visits,ga:newVisits", dimensions: "ga:country"},
{metrics: "ga:visits,ga:newVisits", dimensions: "ga:language"},
{metrics: "ga:visits,ga:newVisits", dimensions: "ga:browser"},
{metrics: "ga:visits,ga:newVisits", dimensions: "ga:screenResolution"},
{metrics: "ga:visits,ga:percentNewVisits,ga:newVisits,ga:visitBounceRate,ga:pageviewsPerVisit,ga:avgTimeOnSite", dimensions: "ga:deviceCategory"},
{metrics: "ga:visits,ga:newVisits", dimensions: "ga:mobileDeviceInfo"},
{metrics: "ga:visits,ga:newVisits", dimensions: "ga:medium"},
{metrics: "ga:visits,ga:avgTimeOnSite,ga:pageviewsPerVisit", dimensions: "ga:medium"},
{metrics: "ga:visits,ga:avgTimeOnSite,ga:pageviewsPerVisit,ga:visitBounceRate,ga:totalEvents,ga:goalCompletionsAll", dimensions: "ga:medium"},
{metrics: "ga:visits,ga:newVisits", dimensions: "ga:keyword"},
{metrics: "ga:visits,ga:pageviews,ga:avgTimeOnSite,ga:pageviewsPerVisit", dimensions: "ga:socialNetwork"},
{metrics: "ga:visits,ga:newVisits", dimensions: "ga:fullReferrer"},
{metrics: "ga:avgPageLoadTime", dimensions: "ga:browser"},
{metrics: "ga:avgPageLoadTime,ga:pageviews,ga:visitBounceRate,ga:exitRate", dimensions: "ga:pagePath"},
{metrics: "ga:avgPageLoadTime,ga:pageviews,ga:visitBounceRate,ga:exitRate", dimensions: "ga:pageTitle"},
{metrics: "ga:pageviews,ga:uniquePageviews,ga:avgTimeOnPage,ga:entrances,ga:visitBounceRate,ga:exitRate", dimensions: "ga:pagePath"},
{metrics: "ga:pageviews,ga:uniquePageviews,ga:avgTimeOnPage,ga:entrances,ga:visitBounceRate,ga:exitRate", dimensions: "ga:pageTitle"},
{metrics: "ga:totalEvents,ga:uniqueEvents"},
{metrics: "ga:visits,ga:goalCompletionsAll"},
];
// Get all informations for the profile (start-date and end-date must be in '' because javascript has problems with - )
for (count = 0; count < requests_list.length; count++) {
var request_object = {
'ids': 'ga:' + page_id,
'start-date': start_date,
'end-date': end_date,
'metrics': requests_list[count].metrics
};
if (requests_list[count].dimensions) {
request_object.dimensions = requests_list[count].dimensions;
}
if (requests_list[count].filters) {
request_object.filters = requests_list[count].filters;
}
var subrequest = client.analytics.data.ga.get(request_object).withAuthClient(oauth2Client);
subrequest.execute( function (err, result) {
if (err) {
utils.applog('error', {from: "lib/fetch/analytics.js", value: err, reference: "getAnalytics->subrequest", resource: user_id});
return;
} else {
// Get errors that are not stored in err variable
if (!result.profileInfo) {
utils.applog('error', {from: "lib/fetch/analytics.js", value: result, reference: "getAnalytics->subrequest->wrongResult", resource: user_id});
return;
}
.................
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment