Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updates and changes to work with 0.4.0. Still an issue with extra dat…

…a causing an error in http...need to track this down.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information...
commit 44fe4420bd3636f73a3e0708b1ac7b4432cfa9a8 1 parent b8a641c
@ncb000gt authored
Showing with 32 additions and 20 deletions.
  1. +3 −1 README.md
  2. +27 −17 lib/ga.js
  3. +2 −2 package.json
View
4 README.md
@@ -5,6 +5,8 @@ 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.
+* Updated for NodeJS 0.4.x *
+
Usage
============
@@ -57,4 +59,4 @@ Each array contains objects. These objects contain the following:
License
============
-see license file
+see license file
View
44 lib/ga.js
@@ -1,7 +1,7 @@
var select = require('soupselect').select
,htmlparser = require('htmlparser')
-,sys = require('sys')
-,http = require('http')
+,util = require('util')
+,https = require('https')
,querystring = require('querystring')
,emitter = require('events').EventEmitter;
@@ -17,10 +17,10 @@ function GA(config) {
throw Error("No config given.");
}
this.token = null;
- this.client = new http.createClient(443, 'www.google.com', true);
+ //this.client = new http.createClient(443, 'www.google.com', true);
};
-sys.inherits(GA, emitter);
+util.inherits(GA, emitter);
exports.GA = GA;
@@ -53,11 +53,9 @@ GA.prototype.login = function(cb) {
service: "analytics"
};
- var request = this.client.request("POST", '/accounts/ClientLogin', {
- "Content-Type": 'application/x-www-form-urlencoded'
- });
- request.end(querystring.stringify(postData));
- request.on('response', function(res) {
+ var options = { host: "www.google.com", port: 443, method: 'POST', path: '/accounts/ClientLogin', headers: { 'Content-Type': 'application/x-www-form-urlencoded' } };
+ var req = https.request(options, function(res) {
+ util.debug('inside response.');
var chunks = [];
var length = 0;
res.on('data', function(chunk) {
@@ -65,7 +63,6 @@ GA.prototype.login = function(cb) {
length += chunk.length;
});
res.on('end', function() {
- request.connection.end();
var data = combineChunks(chunks, length).toString();
var idx = data.indexOf('Auth=');
if (idx < 0 && !data) {
@@ -77,6 +74,9 @@ GA.prototype.login = function(cb) {
}
});
});
+ util.debug('qs: ' + querystring.stringify(postData));
+ req.write(querystring.stringify(postData));
+ req.end();
};
/*
@@ -95,21 +95,30 @@ GA.prototype.get = function(options, cb) {
self.on('entries', cb);
var data_url = "/analytics/feeds/data?" + querystring.stringify(options);
- var data_request = this.client.request("GET", data_url, {
+ //var data_request = this.client.request("GET", data_url, {
+ //Authorization:"GoogleLogin "+this.token,
+ //"GData-Version": 2
+ //});
+
+ var get_options = {
+ host: 'www.google.com',
+ port: 443,
+ path: data_url,
+ method: 'GET',
+ headers: {
Authorization:"GoogleLogin "+this.token,
"GData-Version": 2
- });
+ }
+ };
- data_request.end();
- data_request.on('response', function(data_response) {
+ var req = https.request(get_options, function(res) {
var chunks = [];
var length = 0;
- data_response.on('data', function(chunk) {
+ res.on('data', function(chunk) {
chunks.push(chunk);
length += chunk.length;
});
- data_response.on('end', function() {
- data_response.connection.end();
+ res.on('end', function() {
var data_data = combineChunks(chunks, length).toString();
if (data_data.indexOf("<?xml") == 0) {
var parser = new htmlparser.Parser(
@@ -165,4 +174,5 @@ GA.prototype.get = function(options, cb) {
}
});
});
+ req.end();
};
View
4 package.json
@@ -4,7 +4,7 @@
"description": "Google Analytics library.",
"version": "0.1.1",
"author": "Nick Campbell (http://github.com/ncb000gt)",
- "engines": { "node": ">= 0.1.100" },
+ "engines": { "node": ">= 0.4.0" },
"repository": {
"type": "git",
"url": "http://github.com/ncb000gt/node-googleanalytics.git"
@@ -18,4 +18,4 @@
"soupselect": ">=0.1.0",
"htmlparser": ">=1.6.2"
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.