Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #14 from rowanu/master

Update documentation, added support for (optional) oAuth token to be used (instead of user/password)
  • Loading branch information...
commit 7bf92b80cd39096222fa3e77fed713725c3ded69 2 parents 0844542 + bb1bf8d
@ncb000gt authored
Showing with 53 additions and 28 deletions.
  1. +47 −17 README.md
  2. +6 −11 lib/ga.js
View
64 README.md
@@ -3,7 +3,8 @@ Google Analytics
Pull data from Google Analytics for use in projects.
-The library maintains tracking of the token so that you don't have to and will push the token around with your requests. Should you require a different token, just create a new GA instance. However, this is asynchronous through eventing so if you do want the token you can latch onto the event.
+The library maintains tracking of the token so that you don't have to and will push the token around with your requests.
+Should you require a different token, just create a new GA instance. However, this is asynchronous through eventing so if you do want the token you can latch onto the event.
* Updated for NodeJS 0.6.x *
@@ -11,23 +12,52 @@ The library maintains tracking of the token so that you don't have to and will p
Usage
============
- var ga = require('googleanalytics')
- ,util = require('util');
+With a user and password:
+
+ var GA = require('googleanalytics'),
+ util = require('util'),
+ config = {
+ "user": "myusername",
+ "password": "mypassword"
+ },
+ ga = new GA.GA(config);
+
+ ga.login(function(err, token) {
+ var options = {
+ 'ids': 'ga:<profileid>',
+ 'start-date': '2010-09-01',
+ 'end-date': '2010-09-30',
+ 'dimensions': 'ga:pagePath',
+ 'metrics': 'ga:pageviews',
+ 'sort': '-ga:pagePath'
+ };
+
+ GA.get(options, function(err, entries) {
+ util.debug(JSON.stringify(entries));
+ });
+ });
+
+If you have already gotten permission from a user, you can simply use the oAuth refresh token you have:
+
+ var GA = require('googleanalytics'),
+ util = require('util'),
+ config = {
+ "token": "Auth=XXXXXXXXXXX"
+ },
+ ga = new GA.GA(config);
- var GA = new ga.GA();
- GA.login(function(err, token) {
- var options = {
- 'ids': 'ga:<profileid>',
- 'start-date': '2010-09-01',
- 'end-date': '2010-09-30',
- 'dimensions': 'ga:pagePath',
- 'metrics': 'ga:pageviews',
- 'sort': '-ga:pagePath'
- };
- GA.get(options, function(err, entries) {
- util.debug(JSON.stringify(entries));
- });
- });
+ var options = {
+ 'ids': 'ga:<profileid>',
+ 'start-date': '2010-09-01',
+ 'end-date': '2010-09-30',
+ 'dimensions': 'ga:pagePath',
+ 'metrics': 'ga:pageviews',
+ 'sort': '-ga:pagePath'
+ };
+
+ GA.get(options, function(err, entries) {
+ util.debug(JSON.stringify(entries));
+ });
Rudamentary API
View
17 lib/ga.js
@@ -23,7 +23,7 @@ function GA(config) {
} else {
throw Error("No config given.");
}
- this.token = null;
+ this.token = ('token' in config) ? config.token : null;
};
inherits(GA, emitter);
@@ -49,11 +49,6 @@ GA.prototype.login = function(cb) {
var self = this;
this.on('token', this.captureToken);
- //handle invalid login errors
- this.on('error', function(err){
- cb(err, null);
- });
-
if (cb) this.on('token', cb);
var postData = {
@@ -127,6 +122,8 @@ GA.prototype.get = function(options, cb) {
,dimension_index;
var data_data = combineChunks(chunks, length).toString();
+
+ console.log(data_data)
var parsed_data = JSON.parse(data_data);
@@ -150,10 +147,8 @@ GA.prototype.get = function(options, cb) {
entry.metrics.push(object_metric);
var object_dimension = {};
- if (dimension) {
- object_dimension[dimension.name] = parsed_data.rows[i][dimension_index];
- entry.dimensions.push(object_dimension);
- }
+ object_dimension[dimension.name] = parsed_data.rows[i][dimension_index];
+ entry.dimensions.push(object_dimension);
self.emit('entry', entry);
entries.push(entry);
@@ -161,7 +156,7 @@ GA.prototype.get = function(options, cb) {
}
if (typeof cb === 'function' ) cb(null, entries);
-
+
});
});
req.end();
Please sign in to comment.
Something went wrong with that request. Please try again.