Created
June 9, 2014 12:28
-
-
Save DanisHack/27346246e1f703b502ba to your computer and use it in GitHub Desktop.
Google Analytics Api--getting data in batches in Node (using express)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.AnalyticsDataBatchRequest = function(profileId, startDate, endDate, callback){ | |
var r1 = gapi.mainclient.analytics.data.ga.get({ | |
'ids': 'ga:' + profileId, | |
'start-date': startDate, | |
'end-date': endDate, | |
'metrics': 'ga:pageviews' | |
,'dimensions': 'ga:source, ga:keyword, ga:pagePath' | |
}); | |
var r2 = gapi.mainclient.analytics.data.ga.get({ | |
'ids': 'ga:' + profileId, | |
'start-date': startDate, | |
'end-date':endDate, | |
'metrics': 'ga:pageviews' | |
,'dimensions': 'ga:source, ga:searchKeyword, ga:pagePath' | |
}); | |
var r3 = gapi.mainclient.analytics.data.ga.get({ | |
'ids': 'ga:' + profileId, | |
'start-date': startDate, | |
'end-date':endDate, | |
'metrics': 'ga:pageviews' | |
,'dimensions': 'ga:keyword, ga:source, ga:pagePath' | |
}); | |
var r4 = gapi.mainclient.analytics.data.ga.get({ | |
'ids': 'ga:' + profileId, | |
'start-date': startDate, | |
'end-date':endDate, | |
'metrics': 'ga:pageviews' | |
,'dimensions': 'ga:searchKeyword, ga:source, ga:pagePath' | |
}); | |
var r5 = gapi.mainclient.analytics.data.ga.get({ | |
'ids': 'ga:' + profileId, | |
'start-date': startDate, | |
'end-date': endDate, | |
'metrics': 'ga:visitors, ga:visits, ga:pageviews' | |
,'dimensions': 'ga:visitorType' | |
}); | |
gapi.mainclient.newBatchRequest() | |
.add(r1) | |
.add(r2) | |
.add(r3) | |
.add(r4) | |
.add(r5).withAuthClient(gapi.client).execute(function(err, result){ | |
if(err){ | |
var error = "Error while making batch query in AnalyticsDataBatchSingle"+err.toString(); | |
//console.log("AnalyticsDataBatchSingle", err); | |
callback(error, null); | |
} | |
else{ | |
callback(null, result); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment