Skip to content

Commit 73e6540

Browse files
fix(pageview): window is not defined (#308)
closes #307
1 parent bd5ea44 commit 73e6540

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/api/disable.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import { isBrowser } from "@/utils";
12
import { getOptions } from "@/options";
23

34
const assignGlobalProperty = (id, value) => {
5+
if (!isBrowser()) {
6+
return;
7+
}
8+
49
window[`ga-disable-${id}`] = value;
510
};
611

src/api/pageview.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { getOptions } from "@/options";
2+
import { isBrowser } from "@/utils";
23
import event from "@/api/event";
34

45
export default (param) => {
6+
if (!isBrowser()) {
7+
return;
8+
}
9+
510
let template;
611

712
if (typeof param === "string") {

test/api/pageview.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import flushPromises from "flush-promises";
88
jest.mock("@/api/event");
99

1010
describe("pageview", () => {
11+
const _window = window;
1112
const { location } = window;
1213

1314
beforeAll(() => {
@@ -18,6 +19,10 @@ describe("pageview", () => {
1819
};
1920
});
2021

22+
beforeEach(() => {
23+
global.window = _window;
24+
});
25+
2126
afterAll(() => {
2227
window.location = location;
2328
});
@@ -74,6 +79,20 @@ describe("pageview", () => {
7479
});
7580
});
7681

82+
test("track pageview without window", () => {
83+
const localVue = createLocalVue();
84+
85+
localVue.use(VueGtag, {
86+
config: { id: 1 },
87+
});
88+
89+
delete global.window;
90+
91+
expect(() => {
92+
pageview("/");
93+
}).not.toThrow();
94+
});
95+
7796
describe("pageTrackerUseFullPath", () => {
7897
test("tracks using router `path` property", async () => {
7998
const localVue = createLocalVue();

0 commit comments

Comments
 (0)