Skip to content

Loading…

Add some error handling #17

Merged
merged 4 commits into from

3 participants

@qzaidi

Added error handling to support cases when an incorrect username/password/profileid is used.

@lukewilde

Thanks, this helped me out.

@ncb000gt
Owner

Can you remove the console.log's and then I'll merge this in. Thanks!

@ncb000gt ncb000gt merged commit 01449ae into ncb000gt:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 29, 2012
  1. @qzaidi

    error handling

    qzaidi committed
Commits on Nov 30, 2012
  1. @qzaidi

    bugfix, more metrics in example

    qzaidi committed
  2. @qzaidi

    remove comment

    qzaidi committed
  3. @qzaidi

    removed console.log from lib/ga

    qzaidi committed
Showing with 43 additions and 9 deletions.
  1. +35 −6 examples/basic.js
  2. +8 −3 lib/ga.js
View
41 examples/basic.js
@@ -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
11 lib/ga.js
@@ -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);
Something went wrong with that request. Please try again.