Skip to content

Commit 2fc6f7e

Browse files
fix(bootstrap): double hit when page tracker enabled (#146)
1 parent 9695d22 commit 2fc6f7e

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

__tests__/bootstrap.spec.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import config from "@/api/config";
12
import bootstrap from "@/bootstrap";
2-
import { getOptions } from "@/install";
3+
import { getRouter, getOptions } from "@/install";
34
import flushPromises from "flush-promises";
45
import pageTracker from "@/page-tracker";
56
import optOut from "@/api/opt-out";
@@ -159,6 +160,7 @@ describe("bootstrap", () => {
159160
});
160161

161162
it("should start tracking pages when enabled", () => {
163+
getRouter.mockReturnValueOnce({});
162164
getOptions.mockReturnValueOnce({
163165
globalObjectName: "gtag",
164166
pageTrackerEnabled: true,
@@ -170,11 +172,12 @@ describe("bootstrap", () => {
170172
bootstrap();
171173

172174
expect(pageTracker).toHaveBeenCalled();
175+
expect(config).not.toHaveBeenCalled();
173176

174177
flushPromises();
175178
});
176179

177-
it("should not start tracking pages when enabled", () => {
180+
it("should not start tracking pages when disabled", () => {
178181
getOptions.mockReturnValueOnce({
179182
globalObjectName: "gtag",
180183
pageTrackerEnabled: false,
@@ -190,6 +193,21 @@ describe("bootstrap", () => {
190193
flushPromises();
191194
});
192195

196+
it("should fire a config when pageTracker is not enabled", () => {
197+
getOptions.mockReturnValueOnce({
198+
globalObjectName: "gtag",
199+
config: {
200+
id: 1
201+
}
202+
});
203+
204+
bootstrap();
205+
206+
expect(config).toHaveBeenCalled();
207+
208+
flushPromises();
209+
});
210+
193211
it("should return an error when script loading fails", done => {
194212
util.warn = jest.fn();
195213
util.loadScript = jest.fn(() => Promise.reject(new Error()));

src/bootstrap.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { warn, isFn, loadScript } from "./util";
22
import config from "./api/config";
3-
import { getOptions } from "../src/install";
3+
import { getRouter, getOptions } from "../src/install";
44
import optOut from "./api/opt-out";
55
import pageTracker from "./page-tracker";
66

@@ -18,6 +18,9 @@ export default function() {
1818
disableScriptLoad
1919
} = getOptions();
2020

21+
const Router = getRouter();
22+
const isPageTrackerEnabled = Boolean(pageTrackerEnabled && Router);
23+
2124
if (!enabled) {
2225
optOut();
2326
}
@@ -31,10 +34,10 @@ export default function() {
3134

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

34-
config();
35-
36-
if (pageTrackerEnabled) {
37+
if (isPageTrackerEnabled) {
3738
pageTracker();
39+
} else {
40+
config();
3841
}
3942

4043
if (disableScriptLoad) {

0 commit comments

Comments
 (0)