Skip to content

Commit ec34617

Browse files
committed
replace htmlparser with xmldom and soupselect with jquery, manually define inherits function in place of utils, to make compatible with newest stable version of node
1 parent 95877df commit ec34617

File tree

3 files changed

+35
-45
lines changed

3 files changed

+35
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Pull data from Google Analytics for use in projects.
55

66
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.
77

8-
* Updated for NodeJS 0.4.x *
8+
* Updated for NodeJS 0.6.x *
99

1010

1111
Usage

lib/ga.js

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
var select = require('soupselect').select
2-
,htmlparser = require('htmlparser')
3-
,util = require('util')
4-
,https = require('https')
1+
var https = require('https')
52
,querystring = require('querystring')
6-
,emitter = require('events').EventEmitter;
3+
,emitter = require('events').EventEmitter
4+
,DOMParser= require('xmldom').DOMParser
5+
,$ = require('jquery')
6+
,fs = require('fs')
7+
,inherits = function (ctor, superCtor) {
8+
ctor.super_ = superCtor;
9+
ctor.prototype = Object.create(superCtor.prototype, {
10+
constructor: {
11+
value: ctor,
12+
enumerable: false
13+
}
14+
});
15+
};;
716

817
function GA(config) {
918
if (config) {
@@ -20,7 +29,7 @@ function GA(config) {
2029
//this.client = new http.createClient(443, 'www.google.com', true);
2130
};
2231

23-
util.inherits(GA, emitter);
32+
inherits(GA, emitter);
2433

2534
exports.GA = GA;
2635

@@ -55,7 +64,6 @@ GA.prototype.login = function(cb) {
5564

5665
var options = { host: "www.google.com", port: 443, method: 'POST', path: '/accounts/ClientLogin', headers: { 'Content-Type': 'application/x-www-form-urlencoded' } };
5766
var req = https.request(options, function(res) {
58-
util.debug('inside response.');
5967
var chunks = [];
6068
var length = 0;
6169
res.on('data', function(chunk) {
@@ -74,7 +82,6 @@ GA.prototype.login = function(cb) {
7482
}
7583
});
7684
});
77-
util.debug('qs: ' + querystring.stringify(postData));
7885
req.write(querystring.stringify(postData));
7986
req.end();
8087
};
@@ -119,47 +126,31 @@ GA.prototype.get = function(options, cb) {
119126
length += chunk.length;
120127
});
121128
res.on('end', function() {
129+
var entries = [];
122130
var data_data = combineChunks(chunks, length).toString();
123-
if (data_data.indexOf("<?xml") == 0) {
124-
var parser = new htmlparser.Parser(
125-
new htmlparser.DefaultHandler(function(err, dom) {
126-
if (err) {
127-
this.emit('entries', err);
128-
} else {
129-
var entries = [];
130-
select(dom, 'entry').forEach(function(element) {
131+
var doc = new DOMParser().parseFromString(data_data);
132+
$(doc).find('entry').each(function(){
131133
var entry = {metrics:[], dimensions:[]};
132-
var children = element.children;
133-
var len = children.length;
134-
for (var i = 0; i < len; i++) {
135-
var item = children[i];
136-
if (item.name in {"id":'',"updated":''}) continue;
134+
$(this).children()
135+
.filter(function() {
136+
return ($(this).prop('tagName') === 'dxp:metric'
137+
|| $(this).prop('tagName') === 'dxp:dimension');
138+
}).each(function(){
139+
var item =$(this);
137140
var metric = false;
138-
if (item.name == "dxp:metric") {
141+
if (item.prop('tagName') === "dxp:metric") {
139142
metric = true;
140143
}
141-
var o = {};
142-
if (item.attribs) {
143-
var attrs = item.attribs;
144-
if ('name' in attrs && 'value' in attrs) {
145-
var name = item.attribs['name'];
146-
var value = item.attribs['value'];
147-
o.name = name;
148-
if (isNaN(value)) {
149-
o.value = value;
150-
} else {
151-
o.value = parseInt(value, 10);
152-
}
153-
}
154-
}
155-
if ('name' in o && 'value' in o) {
144+
if (item.attr('name') && item.attr('value')) {
145+
var o = {};
146+
o[item.attr('name')] = item.attr('value');
156147
if (metric) {
157148
entry.metrics.push(o);
158149
} else {
159150
entry.dimensions.push(o);
160151
}
161152
}
162-
}
153+
});
163154
if (entry.metrics.length > 0 || entry.dimensions.length > 0) {
164155
self.emit('entry', entry);
165156
entries.push(entry);
@@ -168,10 +159,6 @@ GA.prototype.get = function(options, cb) {
168159

169160
self.emit('entries', null, entries);
170161
self.removeListener('entries', cb);
171-
}
172-
}));
173-
parser.parseComplete(data_data);
174-
}
175162
});
176163
});
177164
req.end();

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"description": "Google Analytics library.",
55
"version": "0.1.1",
66
"author": "Nick Campbell (https://github.com/ncb000gt)",
7+
"contributors": [
8+
{ "name": "Brian Zeligson (https://github.com/beezee)", "email": "brian.zeligson@gmail.com" },
9+
],
710
"engines": { "node": ">= 0.4.0" },
811
"repository": {
912
"type": "git",
@@ -15,7 +18,7 @@
1518
}
1619
],
1720
"dependencies": {
18-
"soupselect": ">=0.1.0",
19-
"htmlparser": ">=1.6.2"
21+
"xmldom" : ""
22+
,"jquery" : ""
2023
}
2124
}

0 commit comments

Comments
 (0)