11var https = require ( 'https' )
22, querystring = require ( 'querystring' )
33, emitter = require ( 'events' ) . EventEmitter
4- , DOMParser = require ( 'xmldom' ) . DOMParser
5- , $ = require ( 'jquery' )
4+
65, inherits = function ( ctor , superCtor ) {
76 ctor . super_ = superCtor ;
87 ctor . prototype = Object . create ( superCtor . prototype , {
@@ -95,10 +94,11 @@ GA.prototype.login = function(cb) {
9594GA . prototype . get = function ( options , cb ) {
9695 var self = this ;
9796
98- var data_url = "/analytics/feeds/data?" + querystring . stringify ( options ) ;
97+ options [ 'max-results' ] = 10 ;
98+ var data_url = "/analytics/v3/data/ga?" + querystring . stringify ( options ) ;
9999
100100 var get_options = {
101- host : 'www.google .com' ,
101+ host : 'www.googleapis .com' ,
102102 port : 443 ,
103103 path : data_url ,
104104 method : 'GET' ,
@@ -116,38 +116,46 @@ GA.prototype.get = function(options, cb) {
116116 length += chunk . length ;
117117 } ) ;
118118 res . on ( 'end' , function ( ) {
119- var entries = [ ] ;
119+ var entries = [ ]
120+ , metric_index
121+ , metric
122+ , dimension
123+ , dimension_index ;
124+
120125 var data_data = combineChunks ( chunks , length ) . toString ( ) ;
121- var doc = new DOMParser ( ) . parseFromString ( data_data ) ;
122- $ ( doc ) . find ( 'entry' ) . each ( function ( ) {
123- var entry = { metrics :[ ] , dimensions :[ ] } ;
124- $ ( this ) . children ( )
125- . filter ( function ( ) {
126- return ( $ ( this ) . prop ( 'tagName' ) === 'dxp:metric'
127- || $ ( this ) . prop ( 'tagName' ) === 'dxp:dimension' ) ;
128- } ) . each ( function ( ) {
129- var item = $ ( this ) ;
130- var metric = false ;
131- if ( item . prop ( 'tagName' ) === "dxp:metric" ) {
132- metric = true ;
133- }
134- if ( item . attr ( 'name' ) && item . attr ( 'value' ) ) {
135- var o = { } ;
136- o [ item . attr ( 'name' ) ] = ( isNaN ( item . attr ( 'value' ) ) ) ? item . attr ( 'value' ) : parseInt ( item . attr ( 'value' ) , 10 ) ;
137- if ( metric ) {
138- entry . metrics . push ( o ) ;
139- } else {
140- entry . dimensions . push ( o ) ;
141- }
142- }
143- } ) ;
144- if ( entry . metrics . length > 0 || entry . dimensions . length > 0 ) {
145- self . emit ( 'entry' , entry ) ;
146- entries . push ( entry ) ;
147- }
148- } ) ;
149-
150- if ( typeof cb === 'function' ) cb ( null , entries )
126+
127+ var parsed_data = JSON . parse ( data_data ) ;
128+
129+ for ( var col = 0 ; col < parsed_data . columnHeaders . length ; col ++ ) {
130+ if ( parsed_data . columnHeaders [ col ] [ 'columnType' ] === "METRIC" ) {
131+ metric_index = col ;
132+ metric = parsed_data . columnHeaders [ col ] ;
133+ }
134+ if ( parsed_data . columnHeaders [ col ] [ 'columnType' ] === "DIMENSION" ) {
135+ dimension_index = col ;
136+ dimension = parsed_data . columnHeaders [ col ] ;
137+ }
138+ }
139+
140+
141+ for ( i = 0 ; i < parsed_data . rows . length ; i ++ ) {
142+ var entry = { metrics :[ ] , dimensions :[ ] } ;
143+
144+ var object_metric = { } ;
145+ object_metric [ metric . name ] = parseInt ( parsed_data . rows [ i ] [ metric_index ] , 10 ) ;
146+ entry . metrics . push ( object_metric ) ;
147+
148+ var object_dimension = { } ;
149+ object_dimension [ dimension . name ] = parsed_data . rows [ i ] [ dimension_index ] ;
150+ entry . dimensions . push ( object_dimension ) ;
151+
152+ self . emit ( 'entry' , entry ) ;
153+ entries . push ( entry ) ;
154+
155+ }
156+
157+ if ( typeof cb === 'function' ) cb ( null , entries ) ;
158+
151159 } ) ;
152160 } ) ;
153161 req . end ( ) ;
0 commit comments