Skip to content

Commit

Permalink
added comments and addressed some of the comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchiramoitra committed Jun 15, 2020
1 parent b0673d0 commit 7bca60e
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions integrations/GA/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import defaults from "@ndhoule/defaults";
class GA {
constructor(config) {
this.trackingID = config.trackingID;
this.sendUserId = config.sendUserId || false; //from here new to be added in ui
this.sendUserId = config.sendUserId || false;
this.dimensions = config.dimensions || [];
this.metrics = config.metrics || [];
this.contentGroupings = config.contentGroupings || [];
Expand All @@ -19,14 +19,14 @@ class GA {
this.doubleClick = config.doubleClick || false;
this.enhancedEcommerce = config.enhancedEcommerce || false;
this.enhancedLinkAttribution = config.enhancedLinkAttribution || false;

this.includeSearch = config.includeSearch || false;
this.setAllMappedProps = config.setAllMappedProps || true;
this.siteSpeedSampleRate = config.siteSpeedSampleRate || 1;
this.sampleRate = config.sampleRate || 100;
this.trackCategorizedPages = config.trackCategorizedPages || true;
this.trackNamedPages = config.trackNamedPages || true;
this.optimize = config.optimize || "";
this.optimizeContainerId = config.optimize || "";
this.resetCustomDimensionsOnPage = config.resetCustomDimensionsOnPage || [];
this.inputs = config;
this.enhancedEcommerceLoaded = 0;
Expand Down Expand Up @@ -64,6 +64,7 @@ class GA {
};
ga.l = new Date().getTime();

//create ga with these properties. if the properties are empty it will take default values.
var config = {
cookieDomain: this.domain || GA.prototype.defaults.domain,
siteSpeedSampleRate: this.siteSpeedSampleRate,
Expand All @@ -74,18 +75,21 @@ class GA {

ga("create", this.trackingID, config);

if (this.optimize) {
ga("require", this.optimize);
if (this.optimizeContainerId) {
ga("require", this.optimizeContainerId);
}

//this is to display advertising
if (this.doubleClick) {
ga("require", "displayfeatures");
}

// https://support.google.com/analytics/answer/2558867?hl=en
if (this.enhancedLinkAttribution) {
ga("require", "linkid", "linkid.js");
ga("require", "linkid");
}

//a warning is in ga debugger if anonymize is false after initialization
if (this.anonymizeIp) {
ga("set", "anonymizeIp", true);
}
Expand All @@ -106,23 +110,13 @@ class GA {
for (let val of this.contentGroupings) {
contentGroupingsArray[val.from] = val.to;
}
console.log(rudderElement.message.userId);

//send global id
if (this.sendUserId && rudderElement.message.userId) {
ga("set", "userId", rudderElement.message.userId);
}
var dimensionsArray = {};
for (let val of this.dimensions) {
dimensionsArray[val.from] = val.to;
}
var metricsArray = {};
for (let val of this.metrics) {
metricsArray[val.from] = val.to;
}
var contentGroupingsArray = {};
for (let val of this.contentGroupings) {
contentGroupingsArray[val.from] = val.to;
}

//custom dimensions and metrics
var custom = metrics(
rudderElement.message.context.traits,
dimensionsArray,
Expand Down Expand Up @@ -160,13 +154,16 @@ class GA {
var orderId = properties.orderId;
var products = properties.products;

//orderId is required
if (!orderId) return;

//ecommerce is required
if (!this.ecommerce) {
ga("require", "ecommerce");
this.ecommerce = true;
}

//add transaction
ga("ecommerce:addTransaction", {
affiliation: properties.affiliation,
shipping: properties.shipping,
Expand All @@ -176,6 +173,7 @@ class GA {
currency: properties.currency,
});

//products added
each(products, function (product) {
var productTrack = createProductTrack(rudderElement, product);

Expand Down Expand Up @@ -506,6 +504,7 @@ class GA {
eventAction: eventAction,
eventLabel: eventLabel,
eventValue: formatValue(eventValue),
// Allow users to override their nonInteraction integration setting for any single particluar event.
nonInteraction:
rudderElement.message.properties.nonInteraction !== undefined
? !!rudderElement.message.properties.nonInteraction
Expand Down Expand Up @@ -588,6 +587,11 @@ class GA {
page: pagePath,
title: pageTitle,
};
// Reset custom dimension which are previously set.
// Uses the configured dimensions as:
// this.dimensions: { "fruit": "dimension1" }
// this.resetCustomDimensions: [ "fruit" ]
// --> resetCustomDimensions: { "dimension1": null }

var resetCustomDimensions = {};
for (var i = 0; i < this.resetCustomDimensionsOnPage.length; i++) {
Expand All @@ -598,6 +602,8 @@ class GA {
}

ga("set", resetCustomDimensions);

//adds more properties to pageview which will be sent
pageview = extend(
pageview,
setCustomDimenionsAndMetrics(eventProperties, this.inputs)
Expand All @@ -611,10 +617,12 @@ class GA {

ga("send", "pageview", pageview);

//categorized pages
if (category && this.trackCategorizedPages) {
this.track(rudderElement, { nonInteraction: 1 });
}

//named pages
if (name && this.trackNamedPages) {
this.track(rudderElement, { nonInteraction: 1 });
}
Expand All @@ -630,6 +638,19 @@ class GA {
return !!window.gaplugins;
}
}

/**
* Map google's custom dimensions, metrics & content groupings with `obj`.
*
* Example:
*
* metrics({ revenue: 1.9 }, { { metrics : { revenue: 'metric8' } });
* // => { metric8: 1.9 }
*
* metrics({ revenue: 1.9 }, {});
* // => {}
*/

function metrics(obj, dimensions, metrics, contentGroupings) {
var ret = {};

Expand Down Expand Up @@ -678,19 +699,26 @@ function setCustomDimenionsAndMetrics(props, inputs) {
}
}
}

// Return the path based on `properties` and `options`

function path(properties, includeSearch) {
if (!properties) return;
var str = properties.path;
if (includeSearch && properties.search) str += properties.search;
return str;
}

//Creates a track out of product properties.

function createProductTrack(rudderElement, properties) {
var props = properties || {};
props.currency =
properties.currency || rudderElement.message.properties.currency;
return { properties: props };
}

// Loads ec.js (unless already loaded)
function loadEnhancedEcommerce(rudderElement, a) {
if (a === 0) {
ga("require", "ec");
Expand All @@ -700,6 +728,8 @@ function loadEnhancedEcommerce(rudderElement, a) {
ga("set", "&cu", rudderElement.message.properties.currency);
return a;
}

//helper class to not repeat `ec:addProduct`
function enhancedEcommerceTrackProduct(rudderElement, inputs) {
var dimensionsArray = {};
for (let val of inputs.dimensions) {
Expand Down Expand Up @@ -741,12 +771,13 @@ function enhancedEcommerceTrackProduct(rudderElement, inputs) {
ga("ec:addProduct", product);
}

//extracts checkout options
function extractCheckoutOptions(rudderElement) {
var options = [
rudderElement.message.properties.paymentMethod,
rudderElement.message.properties.shippingMethod,
];

//remove all nulls and join with commas.
var valid = rejectArr(options);
return valid.length > 0 ? valid.join(", ") : null;
}
Expand Down Expand Up @@ -781,6 +812,8 @@ function pushEnhancedEcommerce(rudderElement, inputs) {
ga.apply(window, args);
}

//set action with data

function enhancedEcommerceTrackProductAction(
rudderElement,
action,
Expand Down

0 comments on commit 7bca60e

Please sign in to comment.