Skip to content

Commit 3410580

Browse files
committed
feat: add google universal analytics gtag.js plugin
1 parent c97b35b commit 3410580

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

test/e2e/ga.test.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Modules, constants, and variables
2+
// -----------------------------------------------------------------------------
3+
const docsifyInit = require('../helpers/docsify-init');
4+
const { test, expect } = require('./fixtures/docsify-init-fixture');
5+
6+
const gaTagList = [
7+
'AW-YYYYYY', // Google Ads
8+
'DC-ZZZZZZ', // Floodlight
9+
'G-XXXXXX', // Google Analytics 4 (GA4)
10+
'UA-XXXXXX', // Google Universal Analytics (GA3)
11+
];
12+
13+
// Suite
14+
// -----------------------------------------------------------------------------
15+
test.describe('GA Plugin Tests', () => {
16+
// page request listened, print collect url
17+
function pageRequestListened(page) {
18+
// page.on('request', request => {
19+
// if (request.url().indexOf('www.google-analytics.com') !== -1) {
20+
// console.log(request.url());
21+
// }
22+
// });
23+
24+
page.on('response', response => {
25+
const request = response.request();
26+
// googleads.g.doubleclick.net
27+
// www.google-analytics.com
28+
// www.googletagmanager.com
29+
const reg =
30+
/googleads\.g\.doubleclick\.net|www\.google-analytics\.com|www\.googletagmanager\.com/g;
31+
if (request.url().match(reg)) {
32+
console.log(request.url(), response.status());
33+
}
34+
});
35+
}
36+
37+
// Tests
38+
// ---------------------------------------------------------------------------
39+
test('single gtag', async ({ page }) => {
40+
pageRequestListened(page);
41+
42+
const docsifyInitConfig = {
43+
config: {
44+
ga: gaTagList[0],
45+
},
46+
scriptURLs: ['/lib/plugins/ga.min.js'],
47+
styleURLs: ['/lib/themes/vue.css'],
48+
};
49+
50+
await docsifyInit({
51+
...docsifyInitConfig,
52+
});
53+
54+
const $docsify = await page.evaluate(() => window.$docsify);
55+
56+
// Verify config options
57+
expect(typeof $docsify).toEqual('object');
58+
59+
console.log($docsify.ga, $docsify.ga === '');
60+
61+
// Tests
62+
expect($docsify.ga).not.toEqual('');
63+
});
64+
65+
test('multi gtag', async ({ page }) => {
66+
pageRequestListened(page);
67+
68+
const docsifyInitConfig = {
69+
config: {
70+
ga: gaTagList,
71+
},
72+
scriptURLs: ['/lib/plugins/ga.min.js'],
73+
styleURLs: ['/lib/themes/vue.css'],
74+
};
75+
76+
await docsifyInit({
77+
...docsifyInitConfig,
78+
});
79+
80+
const $docsify = await page.evaluate(() => window.$docsify);
81+
82+
// Verify config options
83+
expect(typeof $docsify).toEqual('object');
84+
85+
console.log($docsify.ga, $docsify.ga === '');
86+
87+
// Tests
88+
expect($docsify.ga).not.toEqual('');
89+
});
90+
91+
test('data-ga attribute', async ({ page }) => {
92+
pageRequestListened(page);
93+
94+
// TODO
95+
});
96+
});

0 commit comments

Comments
 (0)