Skip to content

Commit

Permalink
fix(bootstrap): double hit when page tracker enabled (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGabriele authored Jun 17, 2020
1 parent 9695d22 commit 2fc6f7e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
22 changes: 20 additions & 2 deletions __tests__/bootstrap.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import config from "@/api/config";
import bootstrap from "@/bootstrap";
import { getOptions } from "@/install";
import { getRouter, getOptions } from "@/install";
import flushPromises from "flush-promises";
import pageTracker from "@/page-tracker";
import optOut from "@/api/opt-out";
Expand Down Expand Up @@ -159,6 +160,7 @@ describe("bootstrap", () => {
});

it("should start tracking pages when enabled", () => {
getRouter.mockReturnValueOnce({});
getOptions.mockReturnValueOnce({
globalObjectName: "gtag",
pageTrackerEnabled: true,
Expand All @@ -170,11 +172,12 @@ describe("bootstrap", () => {
bootstrap();

expect(pageTracker).toHaveBeenCalled();
expect(config).not.toHaveBeenCalled();

flushPromises();
});

it("should not start tracking pages when enabled", () => {
it("should not start tracking pages when disabled", () => {
getOptions.mockReturnValueOnce({
globalObjectName: "gtag",
pageTrackerEnabled: false,
Expand All @@ -190,6 +193,21 @@ describe("bootstrap", () => {
flushPromises();
});

it("should fire a config when pageTracker is not enabled", () => {
getOptions.mockReturnValueOnce({
globalObjectName: "gtag",
config: {
id: 1
}
});

bootstrap();

expect(config).toHaveBeenCalled();

flushPromises();
});

it("should return an error when script loading fails", done => {
util.warn = jest.fn();
util.loadScript = jest.fn(() => Promise.reject(new Error()));
Expand Down
11 changes: 7 additions & 4 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { warn, isFn, loadScript } from "./util";
import config from "./api/config";
import { getOptions } from "../src/install";
import { getRouter, getOptions } from "../src/install";
import optOut from "./api/opt-out";
import pageTracker from "./page-tracker";

Expand All @@ -18,6 +18,9 @@ export default function() {
disableScriptLoad
} = getOptions();

const Router = getRouter();
const isPageTrackerEnabled = Boolean(pageTrackerEnabled && Router);

if (!enabled) {
optOut();
}
Expand All @@ -31,10 +34,10 @@ export default function() {

window[globalObjectName]("js", new Date());

config();

if (pageTrackerEnabled) {
if (isPageTrackerEnabled) {
pageTracker();
} else {
config();
}

if (disableScriptLoad) {
Expand Down

0 comments on commit 2fc6f7e

Please sign in to comment.