Permalink
Browse files

Merge pull request #17 from qzaidi/master

Add some error handling
  • Loading branch information...
2 parents b5c1503 + 0eebefa commit 01449ae0f4f1e3bebbb4515bd12cf8489f934cf6 @ncb000gt committed Dec 3, 2012
Showing with 43 additions and 9 deletions.
  1. +35 −6 examples/basic.js
  2. +8 −3 lib/ga.js
View
@@ -1,3 +1,5 @@
+"use strict";
+
var ga = require('../lib/ga');
var profile='';
@@ -8,16 +10,43 @@ var GA = new ga.GA({
user: username,
password: password
});
+
+var dimensions = [
+ 'ga:date'
+];
+
+var metrics = [
+ 'ga:pageviews',
+ 'ga:visitors',
+ 'ga:transactions',
+ 'ga:transactionRevenue'
+];
+
GA.login(function(err, token) {
var options = {
'ids': 'ga:'+profile,
- 'start-date': '2012-07-01',
- 'end-date': '2012-08-10',
- 'dimensions': 'ga:pagePath',
- 'metrics': 'ga:pageviews',
- 'sort': '-ga:pagePath'
+ 'start-date': '2012-07-01',
+ 'end-date': '2012-07-02',
+ 'dimensions': dimensions.join(','),
+ 'metrics': metrics.join(','),
+ 'sort': 'ga:date'
};
+
GA.get(options, function(err, entries) {
- console.log(entries);
+ if (!err) {
+ console.log('date,pageviews,visitors,transactions,sales');
+ entries.forEach(function(entry) {
+ var buf = entry.dimensions[0]['ga:date'];
+
+ metrics.forEach(function(metric) {
+ buf += ',' + entry.metrics[0][metric];
+ });
+
+ console.log(buf);
+
+ });
+ } else {
+ console.log(err);
+ }
});
});
View
@@ -123,10 +123,15 @@ GA.prototype.get = function(options, cb) {
var data_data = combineChunks(chunks, length).toString();
- // console.log(data_data)
-
var parsed_data = JSON.parse(data_data);
+ if (parsed_data.error) {
+ var err = new Error(parsed_data.error.message);
+ if (typeof cb === 'function' ) {
+ return cb(err);
+ }
+ }
+
for (var col=0; col<parsed_data.columnHeaders.length; col++){
if(parsed_data.columnHeaders[col]['columnType'] === "METRIC"){
metric_indexes.push(col);
@@ -143,7 +148,7 @@ GA.prototype.get = function(options, cb) {
var entry = {metrics:[], dimensions:[]};
var object_metric = {};
- for (var j=0; j<dimensions.length; j++){
+ for (var j=0; j<metrics.length; j++){
object_metric[metrics[j].name] = parseInt(parsed_data.rows[i][metric_indexes[j]], 10);
}
entry.metrics.push(object_metric);

0 comments on commit 01449ae

Please sign in to comment.