Skip to content
This repository was archived by the owner on Aug 14, 2019. It is now read-only.

Commit a6357a8

Browse files
authored
Merge pull request #73 from segment-integrations/update/ee-custom-dimensions
2 parents b4e744b + 0932e73 commit a6357a8

File tree

4 files changed

+214
-63
lines changed

4 files changed

+214
-63
lines changed

HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2.10.0 / 2018-02-06
2+
=================
3+
4+
* Support custom dimensions for enhanced ecommerce events.
5+
16
2.9.6 / 2017-11-28
27
=================
38

lib/index.js

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var len = require('object-component').length;
1414
var push = require('global-queue')('_gaq');
1515
var reject = require('reject');
1616
var useHttps = require('use-https');
17+
var extend = require('extend');
1718
var user;
1819

1920
/**
@@ -198,6 +199,7 @@ GA.prototype.page = function(page) {
198199
var pagePath = path(props, this.options);
199200
var pageTitle = name || props.title;
200201
var pageReferrer = page.referrer() || '';
202+
var self = this;
201203
var track;
202204

203205
// store for later
@@ -220,18 +222,7 @@ GA.prototype.page = function(page) {
220222
title: pageTitle
221223
};
222224

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));
235226

236227
if (pageReferrer !== document.referrer) payload.referrer = pageReferrer; // allow referrer override if referrer was manually set
237228
window.ga(this._trackerName + 'set', payload);
@@ -291,6 +282,7 @@ GA.prototype.track = function(track, options) {
291282
opts = defaults(opts, interfaceOpts);
292283
var props = track.properties();
293284
var campaign = track.proxy('context.campaign') || {};
285+
var self = this;
294286

295287
var payload = {
296288
eventAction: track.event(),
@@ -307,18 +299,7 @@ GA.prototype.track = function(track, options) {
307299
if (campaign.content) payload.campaignContent = campaign.content;
308300
if (campaign.term) payload.campaignKeyword = campaign.term;
309301

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));
322303

323304
window.ga(this._trackerName + 'send', 'event', payload);
324305
};
@@ -542,6 +523,31 @@ function path(properties, options) {
542523
return str;
543524
}
544525

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+
545551
/**
546552
* Format the value property to Google's liking.
547553
*
@@ -614,7 +620,7 @@ GA.prototype.loadEnhancedEcommerce = function(track) {
614620
* @param {Track} track
615621
*/
616622

617-
GA.prototype.pushEnhancedEcommerce = function(track) {
623+
GA.prototype.pushEnhancedEcommerce = function(track, opts, trackerName) {
618624
var self = this;
619625
// Send a custom non-interaction event to ensure all EE data is pushed.
620626
// Without doing this we'd need to require page display after setting EE data.
@@ -624,7 +630,7 @@ GA.prototype.pushEnhancedEcommerce = function(track) {
624630
track.category() || 'EnhancedEcommerce',
625631
track.event() || 'Action not defined',
626632
track.properties().label,
627-
{ nonInteraction: 1 }
633+
extend({ nonInteraction: 1 }, setCustomDimenionsAndMetrics(track.properties(), opts, trackerName))
628634
]);
629635
window.ga.apply(window, args);
630636
};
@@ -671,6 +677,7 @@ GA.prototype.checkoutStepViewedEnhanced = function(track) {
671677
var props = track.properties();
672678
var options = extractCheckoutOptions(props);
673679
var self = this;
680+
var opts = this.options;
674681

675682
this.loadEnhancedEcommerce(track);
676683

@@ -683,7 +690,7 @@ GA.prototype.checkoutStepViewedEnhanced = function(track) {
683690
option: options || undefined
684691
});
685692

686-
this.pushEnhancedEcommerce(track);
693+
this.pushEnhancedEcommerce(track, opts, self._trackerName);
687694
};
688695

689696
/**
@@ -727,6 +734,7 @@ GA.prototype.orderCompletedEnhanced = function(track) {
727734
var orderId = track.orderId();
728735
var products = track.products();
729736
var props = track.properties();
737+
var opts = this.options;
730738
var self = this;
731739

732740
// orderId is required.
@@ -748,7 +756,7 @@ GA.prototype.orderCompletedEnhanced = function(track) {
748756
coupon: track.coupon()
749757
});
750758

751-
this.pushEnhancedEcommerce(track);
759+
this.pushEnhancedEcommerce(track, opts, self._trackerName);
752760
};
753761

754762
/**
@@ -764,6 +772,7 @@ GA.prototype.orderRefundedEnhanced = function(track) {
764772
var orderId = track.orderId();
765773
var products = track.products();
766774
var self = this;
775+
var opts = this.options;
767776

768777
// orderId is required.
769778
if (!orderId) return;
@@ -783,7 +792,7 @@ GA.prototype.orderRefundedEnhanced = function(track) {
783792
id: orderId
784793
});
785794

786-
this.pushEnhancedEcommerce(track);
795+
this.pushEnhancedEcommerce(track, opts, self._trackerName);
787796
};
788797

789798
/**
@@ -797,10 +806,11 @@ GA.prototype.orderRefundedEnhanced = function(track) {
797806

798807
GA.prototype.productAddedEnhanced = function(track) {
799808
var self = this;
809+
var opts = this.options;
800810

801811
this.loadEnhancedEcommerce(track);
802812
enhancedEcommerceProductAction(track, 'add', null, self._trackerName);
803-
this.pushEnhancedEcommerce(track);
813+
this.pushEnhancedEcommerce(track, opts, self._trackerName);
804814
};
805815

806816
/**
@@ -814,10 +824,11 @@ GA.prototype.productAddedEnhanced = function(track) {
814824

815825
GA.prototype.productRemovedEnhanced = function(track) {
816826
var self = this;
827+
var opts = this.options;
817828

818829
this.loadEnhancedEcommerce(track);
819830
enhancedEcommerceProductAction(track, 'remove', null, self._trackerName);
820-
this.pushEnhancedEcommerce(track);
831+
this.pushEnhancedEcommerce(track, opts, self._trackerName);
821832
};
822833

823834
/**
@@ -833,12 +844,13 @@ GA.prototype.productViewedEnhanced = function(track) {
833844
var props = track.properties();
834845
var data = {};
835846
var self = this;
847+
var opts = this.options;
836848

837849
this.loadEnhancedEcommerce(track);
838850
// list property is optional
839851
if (props.list) data.list = props.list;
840852
enhancedEcommerceProductAction(track, 'detail', data, self._trackerName);
841-
this.pushEnhancedEcommerce(track);
853+
this.pushEnhancedEcommerce(track, opts, self._trackerName);
842854
};
843855

844856
/**
@@ -854,12 +866,13 @@ GA.prototype.productClickedEnhanced = function(track) {
854866
var props = track.properties();
855867
var data = {};
856868
var self = this;
869+
var opts = this.options;
857870

858871
this.loadEnhancedEcommerce(track);
859872
// list property is optional
860873
if (props.list) data.list = props.list;
861874
enhancedEcommerceProductAction(track, 'click', data, self._trackerName);
862-
this.pushEnhancedEcommerce(track);
875+
this.pushEnhancedEcommerce(track, opts, self._trackerName);
863876
};
864877

865878
/**
@@ -874,6 +887,7 @@ GA.prototype.productClickedEnhanced = function(track) {
874887
GA.prototype.promotionViewedEnhanced = function(track) {
875888
var props = track.properties();
876889
var self = this;
890+
var opts = this.options;
877891

878892
this.loadEnhancedEcommerce(track);
879893
window.ga(self._trackerName + 'ec:addPromo', {
@@ -882,7 +896,7 @@ GA.prototype.promotionViewedEnhanced = function(track) {
882896
creative: props.creative,
883897
position: props.position
884898
});
885-
this.pushEnhancedEcommerce(track);
899+
this.pushEnhancedEcommerce(track, opts, self._trackerName);
886900
};
887901

888902
/**
@@ -897,6 +911,7 @@ GA.prototype.promotionViewedEnhanced = function(track) {
897911
GA.prototype.promotionClickedEnhanced = function(track) {
898912
var props = track.properties();
899913
var self = this;
914+
var opts = this.options;
900915

901916
this.loadEnhancedEcommerce(track);
902917
window.ga(self._trackerName + 'ec:addPromo', {
@@ -906,7 +921,7 @@ GA.prototype.promotionClickedEnhanced = function(track) {
906921
position: props.position
907922
});
908923
window.ga(self._trackerName + 'ec:setAction', 'promo_click', {});
909-
this.pushEnhancedEcommerce(track);
924+
this.pushEnhancedEcommerce(track, opts, self._trackerName);
910925
};
911926

912927
/**
@@ -922,6 +937,7 @@ GA.prototype.productListViewedEnhanced = function(track) {
922937
var props = track.properties();
923938
var products = track.products();
924939
var self = this;
940+
var opts = this.options;
925941

926942
this.loadEnhancedEcommerce(track);
927943
each(products, function(product) {
@@ -945,7 +961,7 @@ GA.prototype.productListViewedEnhanced = function(track) {
945961
window.ga(self._trackerName + 'ec:addImpression', impressionObj);
946962
});
947963

948-
this.pushEnhancedEcommerce(track);
964+
this.pushEnhancedEcommerce(track, opts, self._trackerName);
949965
};
950966

951967
/**
@@ -965,6 +981,7 @@ GA.prototype.productListFilteredEnhanced = function(track) {
965981
var filters = props.filters.map(function(obj) { return obj.type + ':' + obj.value;}).join();
966982
var sorts = props.sorts.map(function(obj) { return obj.type + ':' + obj.value;}).join();
967983
var self = this;
984+
var opts = this.options;
968985

969986
this.loadEnhancedEcommerce(track);
970987
each(products, function(product) {
@@ -987,7 +1004,7 @@ GA.prototype.productListFilteredEnhanced = function(track) {
9871004
window.ga(self._trackerName + 'ec:addImpression', impressionObj);
9881005
});
9891006

990-
this.pushEnhancedEcommerce(track);
1007+
this.pushEnhancedEcommerce(track, opts, self._trackerName);
9911008
};
9921009

9931010

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-integration-google-analytics",
33
"description": "The Google Analytics analytics.js integration.",
4-
"version": "2.9.6",
4+
"version": "2.10.0",
55
"keywords": [
66
"analytics.js",
77
"analytics.js-integration",
@@ -26,6 +26,7 @@
2626
"@ndhoule/defaults": "^2.0.1",
2727
"@segment/analytics.js-integration": "^3.1.0",
2828
"component-each": "^0.2.6",
29+
"extend": "^3.0.1",
2930
"global-queue": "^1.0.1",
3031
"is": "^3.1.0",
3132
"obj-case": "^0.2.0",

0 commit comments

Comments
 (0)