Skip to content

Commit

Permalink
feat(bootstrap): adds isolate global dataLayer (#140)
Browse files Browse the repository at this point in the history
Co-authored-by: Matteo Gabriele <[email protected]>
  • Loading branch information
aetheon and MatteoGabriele authored Jun 25, 2020
1 parent e809c13 commit c360cd9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions __tests__/__snapshots__/install.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Object {
"defaultGroupName": "default",
"disableScriptLoad": false,
"enabled": true,
"globalDataLayerName": "dataLayer",
"globalObjectName": "gtag",
"includes": null,
"onAfterTrack": [Function],
Expand Down
14 changes: 13 additions & 1 deletion __tests__/bootstrap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe("bootstrap", () => {

it("should load the gtag.js file", done => {
getOptions.mockReturnValueOnce({
globalDataLayerName: "dataLayer",
globalObjectName: "gtag",
config: {
id: 1
Expand All @@ -34,7 +35,7 @@ describe("bootstrap", () => {

flushPromises().then(() => {
expect(util.loadScript).toHaveBeenCalledWith(
"https://www.googletagmanager.com/gtag/js?id=1",
"https://www.googletagmanager.com/gtag/js?id=1&l=dataLayer",
"https://www.googletagmanager.com"
);
done();
Expand All @@ -44,6 +45,7 @@ describe("bootstrap", () => {
it("should not load the gtag.js file", () => {
getOptions.mockReturnValueOnce({
disableScriptLoad: true,
globalDataLayerName: "dataLayer",
globalObjectName: "gtag",
config: {
id: 1
Expand All @@ -59,6 +61,7 @@ describe("bootstrap", () => {
const spy = jest.fn();

getOptions.mockReturnValueOnce({
globalDataLayerName: "dataLayer",
globalObjectName: "gtag",
onReady: spy,
config: {
Expand All @@ -76,6 +79,7 @@ describe("bootstrap", () => {

it("should have dataLayer and gtag defined", done => {
getOptions.mockReturnValueOnce({
globalDataLayerName: "dataLayer",
globalObjectName: "gtag",
config: {
id: 1
Expand All @@ -93,6 +97,7 @@ describe("bootstrap", () => {

it("should opt-out when plugin has `enabled` set to false", done => {
getOptions.mockReturnValueOnce({
globalDataLayerName: "dataLayer",
globalObjectName: "gtag",
enabled: false,
config: {
Expand All @@ -111,6 +116,7 @@ describe("bootstrap", () => {
it("should load the gtag.js file also when opt-out", done => {
getOptions.mockReturnValueOnce({
enabled: false,
globalDataLayerName: "dataLayer",
globalObjectName: "gtag",
config: {
id: 1
Expand All @@ -128,6 +134,7 @@ describe("bootstrap", () => {
it("should have dataLayer and gtag defined also when opt-out", done => {
getOptions.mockReturnValueOnce({
enabled: false,
globalDataLayerName: "dataLayer",
globalObjectName: "gtag",
config: {
id: 1
Expand All @@ -145,6 +152,7 @@ describe("bootstrap", () => {

it("should inject a custom name for the gtag library", () => {
getOptions.mockReturnValueOnce({
globalDataLayerName: "dataLayer",
globalObjectName: "foo",
config: {
id: 1
Expand All @@ -162,6 +170,7 @@ describe("bootstrap", () => {
it("should start tracking pages when enabled", () => {
getRouter.mockReturnValueOnce({});
getOptions.mockReturnValueOnce({
globalDataLayerName: "dataLayer",
globalObjectName: "gtag",
pageTrackerEnabled: true,
config: {
Expand All @@ -179,6 +188,7 @@ describe("bootstrap", () => {

it("should not start tracking pages when disabled", () => {
getOptions.mockReturnValueOnce({
globalDataLayerName: "dataLayer",
globalObjectName: "gtag",
pageTrackerEnabled: false,
config: {
Expand All @@ -195,6 +205,7 @@ describe("bootstrap", () => {

it("should fire a config when pageTracker is not enabled", () => {
getOptions.mockReturnValueOnce({
globalDataLayerName: "dataLayer",
globalObjectName: "gtag",
config: {
id: 1
Expand All @@ -213,6 +224,7 @@ describe("bootstrap", () => {
util.loadScript = jest.fn(() => Promise.reject(new Error()));

getOptions.mockReturnValueOnce({
globalDataLayerName: "dataLayer",
globalObjectName: "gtag",
pageTrackerEnabled: true,
config: {
Expand Down
7 changes: 4 additions & 3 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function() {
const {
enabled,
globalObjectName,
globalDataLayerName,
config: { id },
pageTrackerEnabled,
onReady,
Expand All @@ -26,9 +27,9 @@ export default function() {
}

if (window[globalObjectName] == null) {
window.dataLayer = window.dataLayer || [];
window[globalDataLayerName] = window[globalDataLayerName] || [];
window[globalObjectName] = function() {
window.dataLayer.push(arguments);
window[globalDataLayerName].push(arguments);
};
}

Expand All @@ -45,7 +46,7 @@ export default function() {
}

const domain = "https://www.googletagmanager.com";
const resource = `${domain}/gtag/js?id=${id}`;
const resource = `${domain}/gtag/js?id=${id}&l=${globalDataLayerName}`;

return loadScript(resource, domain)
.then(() => {
Expand Down
1 change: 1 addition & 0 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let options = {
disableScriptLoad: false,
bootstrap: true,
globalObjectName: "gtag",
globalDataLayerName: "dataLayer",
pageTrackerEnabled: true,
pageTrackerScreenviewEnabled: false,
defaultGroupName: "default",
Expand Down

0 comments on commit c360cd9

Please sign in to comment.