I've found this method to make authentication and collect data from Google Analytics, but when I press the button, which will run the following method, I'm getting an exception: Uncaught TypeError: undefined is not a function
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
console.log(ga);
var profile = '';
var username = '';
var password = '';
var GA = new ga.GA({
user: username,
password: password
});
var dimensions = [
'ga:day'
];
var metrics = [
'ga:pageviews',
'ga:visitors',
'ga:sessions'
];
GA.login(function (err, token) {
var options = {
'ids': 'ga:' + profile,
'start-date': '2014-08-01',
'end-date': '2014-08-31',
'dimensions': dimensions.join(','),
'metrics': metrics.join(','),
'sort': 'ga:day'
};
GA.get(options, function (err, entries) {
if (!err) {
console.log('date,pageviews,visitors,sessions');
entries.forEach(function (entry) {
var buf = entry.dimensions[0]['ga:day'];
metrics.forEach(function (metric) {
buf += ',' + entry.metrics[0][metric];
});
console.log(buf);
});
} else {
console.log(err);
}