diff --git a/src/add-routes-tracker.js b/src/add-routes-tracker.js index c41e2bf..1b5fdb9 100644 --- a/src/add-routes-tracker.js +++ b/src/add-routes-tracker.js @@ -5,18 +5,34 @@ import { getOptions } from "@/options"; import addConfiguration from "@/add-configuration"; import track from "@/track"; +const isRouteExcluded = (route) => { + const { pageTrackerExcludedRoutes: routes } = getOptions(); + return routes.includes(route.path) || routes.includes(route.name); +}; + export default () => { const { onBeforeTrack, onAfterTrack } = getOptions(); const router = getRouter(); router.onReady(() => { Vue.nextTick().then(() => { + const { currentRoute } = router; + addConfiguration(); - track(router.currentRoute); + + if (isRouteExcluded(currentRoute)) { + return; + } + + track(currentRoute); }); router.afterEach((to, from) => { Vue.nextTick().then(() => { + if (isRouteExcluded(to)) { + return; + } + if (isFn(onBeforeTrack)) { onBeforeTrack(to, from); } diff --git a/src/options.js b/src/options.js index 593f290..3ab0c78 100644 --- a/src/options.js +++ b/src/options.js @@ -10,6 +10,7 @@ export const getDefaultParams = () => ({ customResourceURL: "https://www.googletagmanager.com/gtag/js", customPreconnectOrigin: "https://www.googletagmanager.com", deferScriptLoad: false, + pageTrackerExcludedRoutes: [], pageTrackerEnabled: true, enabled: true, disableScriptLoad: false, diff --git a/test/__snapshots__/options.spec.js.snap b/test/__snapshots__/options.spec.js.snap index bce1a61..bf23619 100644 --- a/test/__snapshots__/options.spec.js.snap +++ b/test/__snapshots__/options.spec.js.snap @@ -24,6 +24,7 @@ Object { "onError": null, "onReady": null, "pageTrackerEnabled": true, + "pageTrackerExcludedRoutes": Array [], "pageTrackerScreenviewEnabled": false, "pageTrackerSkipSamePath": true, "pageTrackerTemplate": null, @@ -55,6 +56,7 @@ Object { "onError": null, "onReady": null, "pageTrackerEnabled": true, + "pageTrackerExcludedRoutes": Array [], "pageTrackerScreenviewEnabled": false, "pageTrackerSkipSamePath": true, "pageTrackerTemplate": null, diff --git a/test/add-routes-tracker.spec.js b/test/add-routes-tracker.spec.js index 7c2f264..2df4d69 100644 --- a/test/add-routes-tracker.spec.js +++ b/test/add-routes-tracker.spec.js @@ -235,4 +235,51 @@ describe("page-tracker", () => { expect(onAfterTrackSpy).toHaveBeenCalledTimes(1); }); + + test("remove routes from tracking based on path", async () => { + const localVue = createLocalVue(); + const onAfterTrackSpy = jest.fn(); + const router = new VueRouter({ + mode: "abstract", + routes: [ + { name: "home", path: "/" }, + { path: "/about" }, + { name: "contacts", path: "/contacts" }, + ], + }); + + localVue.use(VueRouter); + + localVue.use( + VueGtag, + { + pageTrackerExcludedRoutes: ["/about", "contacts"], + onAfterTrack: onAfterTrackSpy, + config: { + id: 1, + }, + }, + router + ); + + mount(App, { router, localVue }); + + router.push("/"); + await flushPromises(); + + router.push("/about"); + await flushPromises(); + + router.push("/contacts"); + await flushPromises(); + + expect(track).toHaveBeenNthCalledWith( + 1, + expect.objectContaining({ + path: "/", + }) + ); + + expect(track).toHaveBeenCalledTimes(1); + }); });