Skip to content

Commit

Permalink
fix(bootstrap): extra hit on bootstrap (#142)
Browse files Browse the repository at this point in the history
there is no need to send a config on bootstrap
  • Loading branch information
MatteoGabriele committed Jun 13, 2020
1 parent 656f3ab commit 6586307
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 43 deletions.
22 changes: 0 additions & 22 deletions __tests__/bootstrap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,26 +211,4 @@ describe("bootstrap", () => {
done();
});
});

it("should fire all the includes", () => {
getOptions.mockReturnValueOnce({
globalObjectName: "gtag",
disableScriptLoad: true,
enabled: true,
includes: [{ id: 2 }, { id: 3, params: { foo: 2 } }],
config: {
id: 1,
params: { foo: 1 }
}
});

global.window.gtag = jest.fn();

bootstrap();

const { calls } = global.window.gtag.mock;
expect(calls[1]).toEqual(["config", 1, { foo: 1 }]);
expect(calls[2]).toEqual(["config", 2]);
expect(calls[3]).toEqual(["config", 3, { foo: 2 }]);
});
});
21 changes: 2 additions & 19 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export default function() {
const {
enabled,
globalObjectName,
config: { id, params },
includes,
config,
pageTrackerEnabled,
onReady,
disableScriptLoad
Expand All @@ -31,22 +30,6 @@ export default function() {

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

if (params) {
window[globalObjectName]("config", id, params);
} else {
window[globalObjectName]("config", id);
}

if (Array.isArray(includes)) {
includes.forEach(domain => {
if (domain.params) {
window[globalObjectName]("config", domain.id, domain.params);
} else {
window[globalObjectName]("config", domain.id);
}
});
}

if (pageTrackerEnabled) {
pageTracker();
}
Expand All @@ -56,7 +39,7 @@ export default function() {
}

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

return loadScript(resource, domain)
.then(() => {
Expand Down
8 changes: 6 additions & 2 deletions src/page-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { warn } from "./util";
import pageview from "./api/pageview";
import screenview from "./api/screenview";

export const trackPage = (to, from) => {
export const trackPage = (to = {}, from = {}) => {
if (to.path === from.path) {
return;
}
Expand Down Expand Up @@ -55,7 +55,11 @@ export const init = Router => {
const { onBeforeTrack, onAfterTrack } = getOptions();

/* istanbul ignore next */
Router.onReady(() => {
Router.onReady(current => {
Vue.nextTick().then(() => {
trackPage(current);
});

Router.afterEach((to, from) => {
Vue.nextTick().then(() => {
onBeforeTrack(to, from);
Expand Down

0 comments on commit 6586307

Please sign in to comment.