-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add google analytics gtag.js plugin (#1702)
Co-authored-by: Joe Pea <joe@trusktr.io> Co-authored-by: 沈唁 <52o@qq52o.cn> Co-authored-by: John Hildenbiddle <jhildenbiddle@users.noreply.github.com> Co-authored-by: zhanzhao <zhanzhao@megvii.com>
- Loading branch information
1 parent
a8a25d0
commit 29f3c82
Showing
4 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* eslint-disable no-console */ | ||
// From ./ga.js | ||
|
||
function appendScript(id) { | ||
const script = document.createElement('script'); | ||
script.async = true; | ||
script.src = 'https://www.googletagmanager.com/gtag/js?id=' + id; | ||
document.body.appendChild(script); | ||
} | ||
|
||
// global site tag instance initialized | ||
function initGlobalSiteTag(id) { | ||
appendScript(id); | ||
|
||
window.dataLayer = window.dataLayer || []; | ||
window.gtag = | ||
window.gtag || | ||
function () { | ||
window.dataLayer.push(arguments); | ||
}; | ||
|
||
window.gtag('js', new Date()); | ||
window.gtag('config', id); | ||
} | ||
|
||
// add additional products to your tag | ||
// https://developers.google.com/tag-platform/gtagjs/install | ||
function initAdditionalTag(id) { | ||
window.gtag('config', id); | ||
} | ||
|
||
function init(ids) { | ||
if (Array.isArray(ids)) { | ||
// set the first id to be a global site tag | ||
initGlobalSiteTag(ids[0]); | ||
|
||
// the rest ids | ||
ids.forEach((id, index) => { | ||
if (index > 0) { | ||
initAdditionalTag(id); | ||
} | ||
}); | ||
} else { | ||
initGlobalSiteTag(ids); | ||
} | ||
} | ||
|
||
function collect() { | ||
if (!window.gtag) { | ||
init($docsify.gtag); | ||
} | ||
|
||
// usage: https://developers.google.com/analytics/devguides/collection/gtagjs/pages | ||
window.gtag('event', 'page_view', { | ||
/* eslint-disable camelcase */ | ||
page_title: document.title, | ||
page_location: location.href, | ||
page_path: location.pathname, | ||
/* eslint-disable camelcase */ | ||
}); | ||
} | ||
|
||
const install = function (hook) { | ||
if (!$docsify.gtag) { | ||
console.error('[Docsify] gtag is required.'); | ||
return; | ||
} | ||
|
||
hook.beforeEach(collect); | ||
}; | ||
|
||
$docsify.plugins = [].concat(install, $docsify.plugins); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Modules, constants, and variables | ||
// npm run test:e2e gtag.test.js | ||
// ----------------------------------------------------------------------------- | ||
const docsifyInit = require('../helpers/docsify-init'); | ||
const { test, expect } = require('./fixtures/docsify-init-fixture'); | ||
|
||
const gtagList = [ | ||
'AW-YYYYYY', // Google Ads | ||
'DC-ZZZZZZ', // Floodlight | ||
'G-XXXXXX', // Google Analytics 4 (GA4) | ||
'UA-XXXXXX', // Google Universal Analytics (GA3) | ||
]; | ||
|
||
// Suite | ||
// ----------------------------------------------------------------------------- | ||
test.describe('Gtag Plugin Tests', () => { | ||
// page request listened, print collect url | ||
function pageRequestListened(page) { | ||
page.on('request', request => { | ||
if (request.url().indexOf('www.google-analytics.com') !== -1) { | ||
// console.log(request.url()); | ||
} | ||
}); | ||
|
||
page.on('response', response => { | ||
const request = response.request(); | ||
// googleads.g.doubleclick.net | ||
// www.google-analytics.com | ||
// www.googletagmanager.com | ||
const reg = | ||
/googleads\.g\.doubleclick\.net|www\.google-analytics\.com|www\.googletagmanager\.com/g; | ||
if (request.url().match(reg)) { | ||
// console.log(request.url(), response.status()); | ||
} | ||
}); | ||
} | ||
|
||
// Tests | ||
// --------------------------------------------------------------------------- | ||
test('single gtag', async ({ page }) => { | ||
pageRequestListened(page); | ||
|
||
const docsifyInitConfig = { | ||
config: { | ||
gtag: gtagList[0], | ||
}, | ||
scriptURLs: ['/lib/plugins/gtag.min.js'], | ||
styleURLs: ['/lib/themes/vue.css'], | ||
}; | ||
|
||
await docsifyInit({ | ||
...docsifyInitConfig, | ||
}); | ||
|
||
const $docsify = await page.evaluate(() => window.$docsify); | ||
|
||
// Verify config options | ||
expect(typeof $docsify).toEqual('object'); | ||
|
||
// console.log($docsify.gtag, $docsify.gtag === ''); | ||
|
||
// Tests | ||
expect($docsify.gtag).not.toEqual(''); | ||
}); | ||
|
||
test('multi gtag', async ({ page }) => { | ||
pageRequestListened(page); | ||
|
||
const docsifyInitConfig = { | ||
config: { | ||
gtag: gtagList, | ||
}, | ||
scriptURLs: ['/lib/plugins/gtag.min.js'], | ||
styleURLs: ['/lib/themes/vue.css'], | ||
}; | ||
|
||
await docsifyInit({ | ||
...docsifyInitConfig, | ||
}); | ||
|
||
const $docsify = await page.evaluate(() => window.$docsify); | ||
|
||
// Verify config options | ||
expect(typeof $docsify).toEqual('object'); | ||
|
||
// console.log($docsify.gtag, $docsify.gtag === ''); | ||
|
||
// Tests | ||
expect($docsify.gtag).not.toEqual(''); | ||
}); | ||
|
||
test('data-ga attribute', async ({ page }) => { | ||
pageRequestListened(page); | ||
|
||
// TODO | ||
}); | ||
}); |
29f3c82
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docsify-preview – ./
docsify-preview-git-develop-docsifyjs.vercel.app
docsify-preview.vercel.app
docsifyjs.org
www.docsifyjs.org
docsify-preview-docsifyjs.vercel.app