Skip to content

Commit

Permalink
Merge pull request #143 from rudderlabs/production
Browse files Browse the repository at this point in the history
  • Loading branch information
arnab-p authored Sep 16, 2020
2 parents b1e4f61 + b64e491 commit befa41c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
86 changes: 53 additions & 33 deletions integrations/GA/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class GA {
this.optimizeContainerId = config.optimize || "";
this.resetCustomDimensionsOnPage = config.resetCustomDimensionsOnPage || [];
this.enhancedEcommerceLoaded = 0;
this.namedTracker = config.namedTracker || false;
this.name = "GA";
this.eventWithCategoryFieldProductScoped = [
"product clicked",
Expand Down Expand Up @@ -92,31 +93,38 @@ export default class GA {
useAmpClientId: this.useGoogleAmpClientId,
};

// set tracker name to rudderGATracker if on
if (this.namedTracker) {
config.name = "rudderGATracker";
this.trackerName = "rudderGATracker.";
} else {
this.trackerName = "";
}
window.ga("create", this.trackingID, config);

if (this.optimizeContainerId) {
window.ga("require", this.optimizeContainerId);
window.ga(`${this.trackerName}require`, this.optimizeContainerId);
}

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

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

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

// a warning is in ga debugger if anonymize is false after initialization
if (this.anonymizeIp) {
window.ga("set", "anonymizeIp", true);
window.ga(`${this.trackerName}set`, "anonymizeIp", true);
}

logger.debug("===in init GA===");
Expand All @@ -125,7 +133,11 @@ export default class GA {
identify(rudderElement) {
// send global id
if (this.sendUserId && rudderElement.message.userId) {
window.ga("set", "userId", rudderElement.message.userId);
window.ga(
`${this.trackerName}set`,
"userId",
rudderElement.message.userId
);
}

// custom dimensions and metrics
Expand All @@ -137,7 +149,7 @@ export default class GA {
);

if (Object.keys(custom).length) {
window.ga("set", custom);
window.ga(`${this.trackerName}set`, custom);
}

logger.debug("in GoogleAnalyticsManager identify");
Expand Down Expand Up @@ -170,7 +182,7 @@ export default class GA {
}

// add transaction
window.ga("ecommerce:addTransaction", {
window.ga(`${this.trackerName}ecommerce:addTransaction`, {
affiliation: properties.affiliation,
shipping: properties.shipping,
revenue: total,
Expand All @@ -183,7 +195,7 @@ export default class GA {
products.forEach((product) => {
const productTrack = self.createProductTrack(rudderElement, product);

window.ga("ecommerce:addItem", {
window.ga(`${this.trackerName}ecommerce:addItem`, {
category: productTrack.properties.category,
quantity: productTrack.properties.quantity,
price: productTrack.properties.price,
Expand All @@ -194,7 +206,7 @@ export default class GA {
});
});

window.ga("ecommerce:send");
window.ga(`${this.trackerName}ecommerce:send`);
}

// enhanced ecommerce events
Expand All @@ -211,7 +223,7 @@ export default class GA {
self.enhancedEcommerceTrackProduct(productTrack);
});

window.ga("ec:setAction", "checkout", {
window.ga(`${this.trackerName}ec:setAction`, "checkout", {
step: properties.step || 1,
option: options || undefined,
});
Expand All @@ -230,8 +242,12 @@ export default class GA {

this.loadEnhancedEcommerce(rudderElement);

window.ga("ec:setAction", "checkout_option", params);
window.ga("send", "event", "Checkout", "Option");
window.ga(
`${this.trackerName}ec:setAction`,
"checkout_option",
params
);
window.ga(`${this.trackerName}send`, "event", "Checkout", "Option");
break;
case "Order Completed":
total =
Expand All @@ -250,7 +266,7 @@ export default class GA {
productTrack = { message: productTrack };
self.enhancedEcommerceTrackProduct(productTrack);
});
window.ga("ec:setAction", "purchase", {
window.ga(`${this.trackerName}ec:setAction`, "purchase", {
id: orderId,
affiliation: props.affiliation,
revenue: total,
Expand All @@ -270,7 +286,7 @@ export default class GA {

each(products, (product) => {
const track = { properties: product };
window.ga("ec:addProduct", {
window.ga(`${this.trackerName}ec:addProduct`, {
id:
track.properties.product_id ||
track.properties.id ||
Expand All @@ -279,7 +295,7 @@ export default class GA {
});
});

window.ga("ec:setAction", "refund", {
window.ga(`${this.trackerName}ec:setAction`, "refund", {
id: orderId,
});

Expand Down Expand Up @@ -323,7 +339,7 @@ export default class GA {
break;
case "Promotion Viewed":
this.loadEnhancedEcommerce(rudderElement);
window.ga("ec:addPromo", {
window.ga(`${this.trackerName}ec:addPromo`, {
id: props.promotion_id || props.id,
name: props.name,
creative: props.creative,
Expand All @@ -334,13 +350,13 @@ export default class GA {
case "Promotion Clicked":
this.loadEnhancedEcommerce(rudderElement);

window.ga("ec:addPromo", {
window.ga(`${this.trackerName}ec:addPromo`, {
id: props.promotion_id || props.id,
name: props.name,
creative: props.creative,
position: props.position,
});
window.ga("ec:setAction", "promo_click", {});
window.ga(`${this.trackerName}ec:setAction`, "promo_click", {});
this.pushEnhancedEcommerce(rudderElement);
break;
case "Product List Viewed":
Expand Down Expand Up @@ -379,13 +395,13 @@ export default class GA {
Object.keys(impressionObj).forEach((key) => {
if (impressionObj[key] === undefined) delete impressionObj[key];
});
window.ga("ec:addImpression", impressionObj);
window.ga(`${this.trackerName}ec:addImpression`, impressionObj);
});
this.pushEnhancedEcommerce(rudderElement);
break;
case "Product List Filtered":
props.filters = props.filters || [];
props.sorters = props.sorters || [];
props.sorts = props.sorts || [];
filters = props.filters
.map((obj) => {
return `${obj.type}:${obj.value}`;
Expand Down Expand Up @@ -435,7 +451,7 @@ export default class GA {
Object.keys(impressionObj).forEach((key) => {
if (impressionObj[key] === undefined) delete impressionObj[key];
});
window.ga("ec:addImpression", impressionObj);
window.ga(`${this.trackerName}ec:addImpression`, impressionObj);
});
this.pushEnhancedEcommerce(rudderElement);
break;
Expand Down Expand Up @@ -473,7 +489,7 @@ export default class GA {
),
};

window.ga("send", "event", payload.payload);
window.ga(`${this.trackerName}send`, "event", payload.payload);
logger.debug("in GoogleAnalyticsManager track");
}
} else {
Expand Down Expand Up @@ -508,7 +524,7 @@ export default class GA {
...this.setCustomDimenionsAndMetrics(rudderElement.message.properties),
};

window.ga("send", "event", payload.payload);
window.ga(`${this.trackerName}send`, "event", payload.payload);
logger.debug("in GoogleAnalyticsManager track");
}
}
Expand Down Expand Up @@ -570,7 +586,7 @@ export default class GA {
resetCustomDimensions[this.dimensionsArray[property]] = null;
}
}
window.ga("set", resetCustomDimensions);
window.ga(`${this.trackerName}set`, resetCustomDimensions);

// adds more properties to pageview which will be sent
pageview = {
Expand All @@ -585,11 +601,11 @@ export default class GA {
logger.debug(document.referrer);
if (pageReferrer !== document.referrer) payload.referrer = pageReferrer;

window.ga("set", payload);
window.ga(`${this.trackerName}set`, payload);

if (this.pageCalled) delete pageview.location;

window.ga("send", "pageview", pageview);
window.ga(`${this.trackerName}send`, "pageview", pageview);

// categorized pages
if (category && this.trackCategorizedPages) {
Expand Down Expand Up @@ -664,7 +680,7 @@ export default class GA {
);
if (Object.keys(custom).length) {
if (this.setAllMappedProps) {
window.ga("set", custom);
window.ga(`${this.trackerName}set`, custom);
} else {
Object.keys(custom).forEach((key) => {
ret[key] = custom[key];
Expand Down Expand Up @@ -712,11 +728,15 @@ export default class GA {
*/
loadEnhancedEcommerce(rudderElement) {
if (this.enhancedEcommerceLoaded === 0) {
window.ga("require", "ec");
window.ga(`${this.trackerName}require`, "ec");
this.enhancedEcommerceLoaded = 1;
}

window.ga("set", "&cu", rudderElement.message.properties.currency);
window.ga(
`${this.trackerName}set`,
"&cu",
rudderElement.message.properties.currency
);
}

/**
Expand Down Expand Up @@ -754,7 +774,7 @@ export default class GA {
),
};

window.ga("ec:addProduct", product);
window.ga(`${this.trackerName}ec:addProduct`, product);
}

/**
Expand All @@ -766,7 +786,7 @@ export default class GA {
*/
enhancedEcommerceTrackProductAction(rudderElement, action, data) {
this.enhancedEcommerceTrackProduct(rudderElement);
window.ga("ec:setAction", action, data || {});
window.ga(`${this.trackerName}ec:setAction`, action, data || {});
}

/**
Expand Down Expand Up @@ -833,4 +853,4 @@ export default class GA {
const valid = rejectArr(options);
return valid.length > 0 ? valid.join(", ") : null;
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rudder-analytics",
"version": "1.1.5",
"version": "1.1.6",
"description": "",
"main": "./dist/browser.min.js",
"size-limit": [
Expand Down

0 comments on commit befa41c

Please sign in to comment.