Skip to content

Commit

Permalink
feat(page-tracker): add option to exclude routes (#305)
Browse files Browse the repository at this point in the history
closes #141
  • Loading branch information
MatteoGabriele authored May 3, 2021
1 parent acd7e16 commit bd5ea44
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/add-routes-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/options.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Object {
"onError": null,
"onReady": null,
"pageTrackerEnabled": true,
"pageTrackerExcludedRoutes": Array [],
"pageTrackerScreenviewEnabled": false,
"pageTrackerSkipSamePath": true,
"pageTrackerTemplate": null,
Expand Down Expand Up @@ -55,6 +56,7 @@ Object {
"onError": null,
"onReady": null,
"pageTrackerEnabled": true,
"pageTrackerExcludedRoutes": Array [],
"pageTrackerScreenviewEnabled": false,
"pageTrackerSkipSamePath": true,
"pageTrackerTemplate": null,
Expand Down
47 changes: 47 additions & 0 deletions test/add-routes-tracker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit bd5ea44

Please sign in to comment.