@@ -14,6 +14,7 @@ var len = require('object-component').length;
14
14
var push = require ( 'global-queue' ) ( '_gaq' ) ;
15
15
var reject = require ( 'reject' ) ;
16
16
var useHttps = require ( 'use-https' ) ;
17
+ var extend = require ( 'extend' ) ;
17
18
var user ;
18
19
19
20
/**
@@ -198,6 +199,7 @@ GA.prototype.page = function(page) {
198
199
var pagePath = path ( props , this . options ) ;
199
200
var pageTitle = name || props . title ;
200
201
var pageReferrer = page . referrer ( ) || '' ;
202
+ var self = this ;
201
203
var track ;
202
204
203
205
// store for later
@@ -220,18 +222,7 @@ GA.prototype.page = function(page) {
220
222
title : pageTitle
221
223
} ;
222
224
223
- // custom dimensions, metrics and content groupings
224
- var custom = metrics ( props , opts ) ;
225
- if ( len ( custom ) ) {
226
- if ( opts . setAllMappedProps ) {
227
- window . ga ( this . _trackerName + 'set' , custom ) ;
228
- } else {
229
- // Add custom dimensions / metrics to pageview payload
230
- each ( custom , function ( key , value ) {
231
- pageview [ key ] = value ;
232
- } ) ;
233
- }
234
- }
225
+ pageview = extend ( pageview , setCustomDimenionsAndMetrics ( props , opts , self . _trackerName ) ) ;
235
226
236
227
if ( pageReferrer !== document . referrer ) payload . referrer = pageReferrer ; // allow referrer override if referrer was manually set
237
228
window . ga ( this . _trackerName + 'set' , payload ) ;
@@ -291,6 +282,7 @@ GA.prototype.track = function(track, options) {
291
282
opts = defaults ( opts , interfaceOpts ) ;
292
283
var props = track . properties ( ) ;
293
284
var campaign = track . proxy ( 'context.campaign' ) || { } ;
285
+ var self = this ;
294
286
295
287
var payload = {
296
288
eventAction : track . event ( ) ,
@@ -307,18 +299,7 @@ GA.prototype.track = function(track, options) {
307
299
if ( campaign . content ) payload . campaignContent = campaign . content ;
308
300
if ( campaign . term ) payload . campaignKeyword = campaign . term ;
309
301
310
- // custom dimensions & metrics
311
- var custom = metrics ( props , interfaceOpts ) ;
312
- if ( len ( custom ) ) {
313
- if ( interfaceOpts . setAllMappedProps ) {
314
- window . ga ( this . _trackerName + 'set' , custom ) ;
315
- } else {
316
- // Add custom dimensions / metrics to payload
317
- each ( custom , function ( key , value ) {
318
- payload [ key ] = value ;
319
- } ) ;
320
- }
321
- }
302
+ payload = extend ( payload , setCustomDimenionsAndMetrics ( props , interfaceOpts , self . _trackerName ) ) ;
322
303
323
304
window . ga ( this . _trackerName + 'send' , 'event' , payload ) ;
324
305
} ;
@@ -542,6 +523,31 @@ function path(properties, options) {
542
523
return str ;
543
524
}
544
525
526
+ /**
527
+ * Set custom dimensions and metrics
528
+ *
529
+ * @param {Properties } props
530
+ * @param {Options } opts
531
+ * @param {String } trackerName
532
+ * @return {Object }
533
+ */
534
+
535
+ function setCustomDimenionsAndMetrics ( props , opts , trackerName ) {
536
+ var ret = { } ;
537
+ var custom = metrics ( props , opts ) ;
538
+ if ( len ( custom ) ) {
539
+ if ( opts . setAllMappedProps ) {
540
+ window . ga ( trackerName + 'set' , custom ) ;
541
+ } else {
542
+ // Add custom dimensions / metrics to event payload
543
+ each ( custom , function ( key , value ) {
544
+ ret [ key ] = value ;
545
+ } ) ;
546
+ return ret ;
547
+ }
548
+ }
549
+ }
550
+
545
551
/**
546
552
* Format the value property to Google's liking.
547
553
*
@@ -614,7 +620,7 @@ GA.prototype.loadEnhancedEcommerce = function(track) {
614
620
* @param {Track } track
615
621
*/
616
622
617
- GA . prototype . pushEnhancedEcommerce = function ( track ) {
623
+ GA . prototype . pushEnhancedEcommerce = function ( track , opts , trackerName ) {
618
624
var self = this ;
619
625
// Send a custom non-interaction event to ensure all EE data is pushed.
620
626
// Without doing this we'd need to require page display after setting EE data.
@@ -624,7 +630,7 @@ GA.prototype.pushEnhancedEcommerce = function(track) {
624
630
track . category ( ) || 'EnhancedEcommerce' ,
625
631
track . event ( ) || 'Action not defined' ,
626
632
track . properties ( ) . label ,
627
- { nonInteraction : 1 }
633
+ extend ( { nonInteraction : 1 } , setCustomDimenionsAndMetrics ( track . properties ( ) , opts , trackerName ) )
628
634
] ) ;
629
635
window . ga . apply ( window , args ) ;
630
636
} ;
@@ -671,6 +677,7 @@ GA.prototype.checkoutStepViewedEnhanced = function(track) {
671
677
var props = track . properties ( ) ;
672
678
var options = extractCheckoutOptions ( props ) ;
673
679
var self = this ;
680
+ var opts = this . options ;
674
681
675
682
this . loadEnhancedEcommerce ( track ) ;
676
683
@@ -683,7 +690,7 @@ GA.prototype.checkoutStepViewedEnhanced = function(track) {
683
690
option : options || undefined
684
691
} ) ;
685
692
686
- this . pushEnhancedEcommerce ( track ) ;
693
+ this . pushEnhancedEcommerce ( track , opts , self . _trackerName ) ;
687
694
} ;
688
695
689
696
/**
@@ -727,6 +734,7 @@ GA.prototype.orderCompletedEnhanced = function(track) {
727
734
var orderId = track . orderId ( ) ;
728
735
var products = track . products ( ) ;
729
736
var props = track . properties ( ) ;
737
+ var opts = this . options ;
730
738
var self = this ;
731
739
732
740
// orderId is required.
@@ -748,7 +756,7 @@ GA.prototype.orderCompletedEnhanced = function(track) {
748
756
coupon : track . coupon ( )
749
757
} ) ;
750
758
751
- this . pushEnhancedEcommerce ( track ) ;
759
+ this . pushEnhancedEcommerce ( track , opts , self . _trackerName ) ;
752
760
} ;
753
761
754
762
/**
@@ -764,6 +772,7 @@ GA.prototype.orderRefundedEnhanced = function(track) {
764
772
var orderId = track . orderId ( ) ;
765
773
var products = track . products ( ) ;
766
774
var self = this ;
775
+ var opts = this . options ;
767
776
768
777
// orderId is required.
769
778
if ( ! orderId ) return ;
@@ -783,7 +792,7 @@ GA.prototype.orderRefundedEnhanced = function(track) {
783
792
id : orderId
784
793
} ) ;
785
794
786
- this . pushEnhancedEcommerce ( track ) ;
795
+ this . pushEnhancedEcommerce ( track , opts , self . _trackerName ) ;
787
796
} ;
788
797
789
798
/**
@@ -797,10 +806,11 @@ GA.prototype.orderRefundedEnhanced = function(track) {
797
806
798
807
GA . prototype . productAddedEnhanced = function ( track ) {
799
808
var self = this ;
809
+ var opts = this . options ;
800
810
801
811
this . loadEnhancedEcommerce ( track ) ;
802
812
enhancedEcommerceProductAction ( track , 'add' , null , self . _trackerName ) ;
803
- this . pushEnhancedEcommerce ( track ) ;
813
+ this . pushEnhancedEcommerce ( track , opts , self . _trackerName ) ;
804
814
} ;
805
815
806
816
/**
@@ -814,10 +824,11 @@ GA.prototype.productAddedEnhanced = function(track) {
814
824
815
825
GA . prototype . productRemovedEnhanced = function ( track ) {
816
826
var self = this ;
827
+ var opts = this . options ;
817
828
818
829
this . loadEnhancedEcommerce ( track ) ;
819
830
enhancedEcommerceProductAction ( track , 'remove' , null , self . _trackerName ) ;
820
- this . pushEnhancedEcommerce ( track ) ;
831
+ this . pushEnhancedEcommerce ( track , opts , self . _trackerName ) ;
821
832
} ;
822
833
823
834
/**
@@ -833,12 +844,13 @@ GA.prototype.productViewedEnhanced = function(track) {
833
844
var props = track . properties ( ) ;
834
845
var data = { } ;
835
846
var self = this ;
847
+ var opts = this . options ;
836
848
837
849
this . loadEnhancedEcommerce ( track ) ;
838
850
// list property is optional
839
851
if ( props . list ) data . list = props . list ;
840
852
enhancedEcommerceProductAction ( track , 'detail' , data , self . _trackerName ) ;
841
- this . pushEnhancedEcommerce ( track ) ;
853
+ this . pushEnhancedEcommerce ( track , opts , self . _trackerName ) ;
842
854
} ;
843
855
844
856
/**
@@ -854,12 +866,13 @@ GA.prototype.productClickedEnhanced = function(track) {
854
866
var props = track . properties ( ) ;
855
867
var data = { } ;
856
868
var self = this ;
869
+ var opts = this . options ;
857
870
858
871
this . loadEnhancedEcommerce ( track ) ;
859
872
// list property is optional
860
873
if ( props . list ) data . list = props . list ;
861
874
enhancedEcommerceProductAction ( track , 'click' , data , self . _trackerName ) ;
862
- this . pushEnhancedEcommerce ( track ) ;
875
+ this . pushEnhancedEcommerce ( track , opts , self . _trackerName ) ;
863
876
} ;
864
877
865
878
/**
@@ -874,6 +887,7 @@ GA.prototype.productClickedEnhanced = function(track) {
874
887
GA . prototype . promotionViewedEnhanced = function ( track ) {
875
888
var props = track . properties ( ) ;
876
889
var self = this ;
890
+ var opts = this . options ;
877
891
878
892
this . loadEnhancedEcommerce ( track ) ;
879
893
window . ga ( self . _trackerName + 'ec:addPromo' , {
@@ -882,7 +896,7 @@ GA.prototype.promotionViewedEnhanced = function(track) {
882
896
creative : props . creative ,
883
897
position : props . position
884
898
} ) ;
885
- this . pushEnhancedEcommerce ( track ) ;
899
+ this . pushEnhancedEcommerce ( track , opts , self . _trackerName ) ;
886
900
} ;
887
901
888
902
/**
@@ -897,6 +911,7 @@ GA.prototype.promotionViewedEnhanced = function(track) {
897
911
GA . prototype . promotionClickedEnhanced = function ( track ) {
898
912
var props = track . properties ( ) ;
899
913
var self = this ;
914
+ var opts = this . options ;
900
915
901
916
this . loadEnhancedEcommerce ( track ) ;
902
917
window . ga ( self . _trackerName + 'ec:addPromo' , {
@@ -906,7 +921,7 @@ GA.prototype.promotionClickedEnhanced = function(track) {
906
921
position : props . position
907
922
} ) ;
908
923
window . ga ( self . _trackerName + 'ec:setAction' , 'promo_click' , { } ) ;
909
- this . pushEnhancedEcommerce ( track ) ;
924
+ this . pushEnhancedEcommerce ( track , opts , self . _trackerName ) ;
910
925
} ;
911
926
912
927
/**
@@ -922,6 +937,7 @@ GA.prototype.productListViewedEnhanced = function(track) {
922
937
var props = track . properties ( ) ;
923
938
var products = track . products ( ) ;
924
939
var self = this ;
940
+ var opts = this . options ;
925
941
926
942
this . loadEnhancedEcommerce ( track ) ;
927
943
each ( products , function ( product ) {
@@ -945,7 +961,7 @@ GA.prototype.productListViewedEnhanced = function(track) {
945
961
window . ga ( self . _trackerName + 'ec:addImpression' , impressionObj ) ;
946
962
} ) ;
947
963
948
- this . pushEnhancedEcommerce ( track ) ;
964
+ this . pushEnhancedEcommerce ( track , opts , self . _trackerName ) ;
949
965
} ;
950
966
951
967
/**
@@ -965,6 +981,7 @@ GA.prototype.productListFilteredEnhanced = function(track) {
965
981
var filters = props . filters . map ( function ( obj ) { return obj . type + ':' + obj . value ; } ) . join ( ) ;
966
982
var sorts = props . sorts . map ( function ( obj ) { return obj . type + ':' + obj . value ; } ) . join ( ) ;
967
983
var self = this ;
984
+ var opts = this . options ;
968
985
969
986
this . loadEnhancedEcommerce ( track ) ;
970
987
each ( products , function ( product ) {
@@ -987,7 +1004,7 @@ GA.prototype.productListFilteredEnhanced = function(track) {
987
1004
window . ga ( self . _trackerName + 'ec:addImpression' , impressionObj ) ;
988
1005
} ) ;
989
1006
990
- this . pushEnhancedEcommerce ( track ) ;
1007
+ this . pushEnhancedEcommerce ( track , opts , self . _trackerName ) ;
991
1008
} ;
992
1009
993
1010
0 commit comments