From e4ca65d368b7d5fca903ccd193dae145f378be7d Mon Sep 17 00:00:00 2001 From: Matteo Gabriele Date: Tue, 14 Jul 2020 12:46:16 +0200 Subject: [PATCH] feat(page-tracker): add pageTrackerSkipSamePath closes #103 --- __tests__/__snapshots__/install.spec.js.snap | 1 + __tests__/page-tracker.spec.js | 25 +++++++++++++++++--- src/install.js | 4 +++- src/page-tracker.js | 16 ++++++++----- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/__tests__/__snapshots__/install.spec.js.snap b/__tests__/__snapshots__/install.spec.js.snap index 4b63abd..af32ab9 100644 --- a/__tests__/__snapshots__/install.spec.js.snap +++ b/__tests__/__snapshots__/install.spec.js.snap @@ -15,6 +15,7 @@ Object { "onReady": [Function], "pageTrackerEnabled": true, "pageTrackerScreenviewEnabled": false, + "pageTrackerSkipSamePath": true, "pageTrackerTemplate": [Function], } `; diff --git a/__tests__/page-tracker.spec.js b/__tests__/page-tracker.spec.js index 39f15cd..f08ef6e 100644 --- a/__tests__/page-tracker.spec.js +++ b/__tests__/page-tracker.spec.js @@ -64,13 +64,32 @@ describe("page-tracker", () => { const to = { name: "home", path: "/" }; const from = { name: "home", path: "/" }; + updateLocationPath("http://localhost/"); + + getOptions.mockReturnValue({ + pageTrackerTemplate: () => null, + pageTrackerSkipSamePath: true + }); + + pageTracker.trackPage({ to, from }); + + expect(pageview).not.toHaveBeenCalled(); + }); + + it("should track the same path", () => { + const to = { name: "home", path: "/" }; + const from = { name: "home", path: "/" }; + updateLocationPath("http://localhost/about"); - getOptions.mockReturnValue({}); + getOptions.mockReturnValue({ + pageTrackerTemplate: () => null, + pageTrackerSkipSamePath: false + }); pageTracker.trackPage({ to, from }); - expect(screenview).not.toHaveBeenCalled(); + expect(pageview).toHaveBeenCalled(); }); it("should warn when using screenview without an appName", () => { @@ -136,7 +155,7 @@ describe("page-tracker", () => { it("should trigger init", () => { const spy = jest.fn(); - getOptions.mockReturnValueOnce({ + getOptions.mockReturnValue({ onBeforeTrack: () => {}, onAfterTrack: () => {} }); diff --git a/src/install.js b/src/install.js index 55f250f..1f67323 100644 --- a/src/install.js +++ b/src/install.js @@ -4,7 +4,8 @@ import bootstrap from "./bootstrap"; let Vue; let Router; -let options = { + +export let options = { pageTrackerTemplate: noop, onBeforeTrack: noop, onAfterTrack: noop, @@ -16,6 +17,7 @@ let options = { globalDataLayerName: "dataLayer", pageTrackerEnabled: true, pageTrackerScreenviewEnabled: false, + pageTrackerSkipSamePath: true, defaultGroupName: "default", includes: null, config: null diff --git a/src/page-tracker.js b/src/page-tracker.js index 3ad339e..77beb9d 100644 --- a/src/page-tracker.js +++ b/src/page-tracker.js @@ -4,10 +4,6 @@ import pageview from "./api/pageview"; import screenview from "./api/screenview"; export const getPageviewTemplate = (to = {}, from = {}) => { - if (to.path === from.path) { - return; - } - const { pageTrackerTemplate, pageTrackerScreenviewEnabled, @@ -35,8 +31,16 @@ export const getPageviewTemplate = (to = {}, from = {}) => { return template; }; -export const trackPage = ({ to, from, params = {} } = {}) => { - const { pageTrackerScreenviewEnabled } = getOptions(); +export const trackPage = ({ to = {}, from = {}, params = {} } = {}) => { + const { + pageTrackerSkipSamePath, + pageTrackerScreenviewEnabled + } = getOptions(); + + if (pageTrackerSkipSamePath && to.path === from.path) { + return; + } + const newParams = { ...getPageviewTemplate(to, from), ...params