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

Commit

Permalink
fix(screenview): missing config hit (#191)
Browse files Browse the repository at this point in the history
closes #178
  • Loading branch information
MatteoGabriele authored Sep 9, 2020
1 parent efce686 commit 0223f52
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 19 deletions.
8 changes: 7 additions & 1 deletion __tests__/__snapshots__/install.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

exports[`install should have default options 1`] = `
Object {
"appName": null,
"bootstrap": true,
"config": null,
"config": Object {
"id": null,
"params": Object {
"send_page_view": false,
},
},
"customPreconnectOrigin": "https://www.googletagmanager.com",
"customResourceURL": "https://www.googletagmanager.com/gtag/js",
"defaultGroupName": "default",
Expand Down
14 changes: 10 additions & 4 deletions __tests__/api/pageview.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import pageview from "@/api/pageview";
import config from "@/api/config";
import event from "@/api/event";

jest.mock("@/api/config");
jest.mock("@/api/event");

describe("api/pageview", () => {
it("should be called with this parameters", () => {
pageview("foo");
expect(config).toHaveBeenCalledWith({

expect(event).toHaveBeenCalledWith("page_view", {
page_path: "foo",
page_location: "http://localhost/",
send_page_view: true,
});

pageview({ foo: "bar" });
expect(config).toHaveBeenCalledWith({ foo: "bar" });

expect(event).toHaveBeenCalledWith("page_view", {
foo: "bar",
send_page_view: true,
});
});
});
27 changes: 24 additions & 3 deletions __tests__/api/screenview.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
import { getOptions } from "@/install";
import screenview from "@/api/screenview";
import event from "@/api/event";

jest.mock("@/api/event");
jest.mock("@/install");

describe("api/screenview", () => {
afterEach(() => {
jest.clearAllMocks();
});

it("should be called with this parameters", () => {
screenview("foo");
expect(event).toHaveBeenCalledWith("screen_view", "foo");
getOptions.mockReturnValue({
config: { id: 1 },
appName: "MyApp",
});

screenview("bar");

expect(event).toHaveBeenCalledWith("screen_view", {
send_page_view: true,
screen_name: "bar",
app_name: "MyApp",
});

screenview({ foo: "bar" });
expect(event).toHaveBeenCalledWith("screen_view", { foo: "bar" });

expect(event).toHaveBeenCalledWith("screen_view", {
send_page_view: true,
foo: "bar",
app_name: "MyApp",
});
});
});
8 changes: 4 additions & 4 deletions __tests__/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ describe("mergeDeep", () => {

describe("warn", () => {
it("should warn with a customized message prefix", () => {
console.warn = jest.fn();
console.error = jest.fn();
util.warn("foo");
expect(console.warn).toHaveBeenCalledWith("[vue-gtag] foo");
expect(console.error).toHaveBeenCalledWith("[vue-gtag] foo");
});

it("should console the error", () => {
console.warn = jest.fn();
console.error = jest.fn();
util.warn("foo", new Error());
expect(console.warn).toHaveBeenCalledTimes(2);
expect(console.error).toHaveBeenCalledTimes(2);
});
});

Expand Down
8 changes: 6 additions & 2 deletions src/api/pageview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import config from "./config";
import event from "./event";

export default (...args) => {
const [arg] = args;
Expand All @@ -13,5 +13,9 @@ export default (...args) => {
params = arg;
}

config(params);
if (params.send_page_view == null) {
params.send_page_view = true;
}

event("page_view", params);
};
23 changes: 22 additions & 1 deletion src/api/screenview.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { getOptions } from "../install";
import event from "./event";

export default (...args) => {
event("screen_view", ...args);
const { appName } = getOptions();
const [arg] = args;
let params = {};

if (typeof arg === "string") {
params = {
screen_name: arg,
};
} else {
params = arg;
}

if (params.app_name == null) {
params.app_name = appName;
}

if (params.send_page_view == null) {
params.send_page_view = true;
}

event("screen_view", params);
};
8 changes: 7 additions & 1 deletion src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ export let options = {
pageTrackerSkipSamePath: true,
defaultGroupName: "default",
includes: null,
config: null,
appName: null,
config: {
id: null,
params: {
send_page_view: false,
},
},
};

export const getOptions = () => options;
Expand Down
4 changes: 3 additions & 1 deletion src/page-tracker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getVue, getRouter, getOptions } from "./install";
import { warn } from "./util";
import api from "./api";
import pageview from "./api/pageview";
import screenview from "./api/screenview";

Expand Down Expand Up @@ -71,7 +72,8 @@ export const startRouter = (Router) => {
/* istanbul ignore next */
Router.onReady((current) => {
Vue.nextTick().then(() => {
trackPage({ to: current, params: config.params });
api.config(config.params);
trackPage({ to: current });
});

Router.afterEach((to, from) => {
Expand Down
4 changes: 2 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export function loadScript(url, preconnectOrigin) {
}

export function warn(msg, err) {
console.warn("[vue-gtag] " + msg);
console.error("[vue-gtag] " + msg);

if (err && err.stack) {
console.warn(err.stack);
console.error(err.stack);
}
}

Expand Down

0 comments on commit 0223f52

Please sign in to comment.