Skip to content

Commit

Permalink
feat(page-tracker): prepend router base (#306)
Browse files Browse the repository at this point in the history
closes #160
  • Loading branch information
MatteoGabriele authored May 3, 2021
1 parent 73e6540 commit 391a32f
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 28 deletions.
13 changes: 10 additions & 3 deletions src/api/pageview.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getOptions } from "@/options";
import { isBrowser } from "@/utils";
import { getRouter } from "@/router";
import { getPathWithBase, isBrowser } from "@/utils";
import event from "@/api/event";

export default (param) => {
Expand All @@ -14,11 +15,17 @@ export default (param) => {
page_path: param,
};
} else if (param.path || param.fullPath) {
const { pageTrackerUseFullPath } = getOptions();
const {
pageTrackerUseFullPath: useFullPath,
pageTrackerPrependBase: useBase,
} = getOptions();
const router = getRouter();
const base = router && router.options.base;
const path = useFullPath ? param.fullPath : param.path;

template = {
...(param.name && { page_title: param.name }),
page_path: pageTrackerUseFullPath ? param.fullPath : param.path,
page_path: useBase ? getPathWithBase(path, base) : path,
};
} else {
template = param;
Expand Down
1 change: 1 addition & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const getDefaultParams = () => ({
pageTrackerScreenviewEnabled: false,
appName: null,
pageTrackerUseFullPath: false,
pageTrackerPrependBase: true,
pageTrackerSkipSamePath: true,
globalDataLayerName: "dataLayer",
globalObjectName: "gtag",
Expand Down
38 changes: 14 additions & 24 deletions src/track.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
import { getOptions } from "@/options";
import { warn, isFn } from "@/utils";
import { validateScreenviewShape, isFn } from "@/utils";
import * as api from "@/api";

export default (to = {}, from = {}) => {
const {
appName,
pageTrackerTemplate,
pageTrackerScreenviewEnabled,
pageTrackerSkipSamePath,
pageTrackerTemplate: proxy,
pageTrackerScreenviewEnabled: useScreenview,
pageTrackerSkipSamePath: skipSamePath,
} = getOptions();

let template = to;

if (isFn(pageTrackerTemplate)) {
template = pageTrackerTemplate(to, from);
} else if (pageTrackerScreenviewEnabled) {
warn(
`Missing "appName" property inside the plugin options.`,
appName == null
);
if (skipSamePath && to.path === from.path) {
return;
}

warn(
`Missing "name" property in the route with path value "${to.path}".`,
to.name == null
);
let template = to;

template = {
if (isFn(proxy)) {
template = proxy(to, from);
} else if (useScreenview) {
template = validateScreenviewShape({
app_name: appName,
screen_name: to.name,
};
}

if (pageTrackerSkipSamePath && to.path === from.path) {
return;
});
}

if (pageTrackerScreenviewEnabled) {
if (useScreenview) {
api.screenview(template);
return;
}
Expand Down
22 changes: 22 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,25 @@ export const warn = (text, shouldLog = true) => {

console.warn(`[vue-gtag] ${text}`);
};

export const validateScreenviewShape = (obj = {}) => {
warn(
`Missing "appName" property inside the plugin options.`,
obj.app_name == null
);

warn(`Missing "name" property in the route.`, obj.screen_name == null);

return obj;
};

export function getPathWithBase(path = "", base = "") {
const pathAsArray = path.split("/");
const baseAsArray = base.split("/");

if (pathAsArray[0] === "" && base[base.length - 1] === "/") {
pathAsArray.shift();
}

return baseAsArray.join("/") + pathAsArray.join("/");
}
2 changes: 2 additions & 0 deletions test/__snapshots__/options.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Object {
"onReady": null,
"pageTrackerEnabled": true,
"pageTrackerExcludedRoutes": Array [],
"pageTrackerPrependBase": true,
"pageTrackerScreenviewEnabled": false,
"pageTrackerSkipSamePath": true,
"pageTrackerTemplate": null,
Expand Down Expand Up @@ -57,6 +58,7 @@ Object {
"onReady": null,
"pageTrackerEnabled": true,
"pageTrackerExcludedRoutes": Array [],
"pageTrackerPrependBase": true,
"pageTrackerScreenviewEnabled": false,
"pageTrackerSkipSamePath": true,
"pageTrackerTemplate": null,
Expand Down
64 changes: 64 additions & 0 deletions test/api/pageview.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,68 @@ describe("pageview", () => {
});
});
});

describe("router base path", () => {
test("use with router installed", async () => {
const localVue = createLocalVue();
const router = new VueRouter({
mode: "abstract",
base: "/app/",
routes: [{ path: "/" }, { path: "/about" }],
});

localVue.use(VueRouter);
localVue.use(
VueGtag,
{
pageTrackerPrependBase: true,
config: {
id: 1,
},
},
router
);

router.push("/about");

await flushPromises();

pageview(router.currentRoute);

expect(event).toHaveBeenCalledWith("page_view", {
send_page_view: true,
page_path: "/app/about",
page_location: "window_location_href_value",
});
});

test("use without router installed", async () => {
const localVue = createLocalVue();
const router = new VueRouter({
mode: "abstract",
base: "/app/",
routes: [{ path: "/" }, { path: "/about" }],
});

localVue.use(VueRouter);
localVue.use(VueGtag, {
pageTrackerPrependBase: true,
config: {
id: 1,
},
});

router.push("/about");

await flushPromises();

pageview(router.currentRoute);

expect(event).toHaveBeenCalledWith("page_view", {
send_page_view: true,
page_path: "/about",
page_location: "window_location_href_value",
});
});
});
});
2 changes: 1 addition & 1 deletion test/track.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe("track", () => {
await flushPromises();

expect(console.warn).toHaveBeenCalledWith(
`[vue-gtag] Missing "name" property in the route with path value "/about".`
`[vue-gtag] Missing "name" property in the route.`
);
});
});
Expand Down

0 comments on commit 391a32f

Please sign in to comment.