Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
feat(page-tracker): add pageTrackerSkipSamePath
Browse files Browse the repository at this point in the history
closes #103
  • Loading branch information
MatteoGabriele committed Jul 14, 2020
1 parent 93c17e1 commit e4ca65d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 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 @@ -15,6 +15,7 @@ Object {
"onReady": [Function],
"pageTrackerEnabled": true,
"pageTrackerScreenviewEnabled": false,
"pageTrackerSkipSamePath": true,
"pageTrackerTemplate": [Function],
}
`;
25 changes: 22 additions & 3 deletions __tests__/page-tracker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -136,7 +155,7 @@ describe("page-tracker", () => {
it("should trigger init", () => {
const spy = jest.fn();

getOptions.mockReturnValueOnce({
getOptions.mockReturnValue({
onBeforeTrack: () => {},
onAfterTrack: () => {}
});
Expand Down
4 changes: 3 additions & 1 deletion src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import bootstrap from "./bootstrap";

let Vue;
let Router;
let options = {

export let options = {
pageTrackerTemplate: noop,
onBeforeTrack: noop,
onAfterTrack: noop,
Expand All @@ -16,6 +17,7 @@ let options = {
globalDataLayerName: "dataLayer",
pageTrackerEnabled: true,
pageTrackerScreenviewEnabled: false,
pageTrackerSkipSamePath: true,
defaultGroupName: "default",
includes: null,
config: null
Expand Down
16 changes: 10 additions & 6 deletions src/page-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e4ca65d

Please sign in to comment.