-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat: add google analytics gtag.js plugin #1702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
6554fb8
b3eeeed
b22e866
db4a103
515fbdc
5a05b30
7fc9b52
b3a8703
9e43564
c97b35b
3410580
456f844
6a3bd84
9a854ec
d33b805
390dd64
9b34990
6fc4933
44da1a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,61 @@ | ||
/* eslint-disable no-console */ | ||
// From https://github.com/egoist/vue-ga/blob/master/src/index.js | ||
function appendScript() { | ||
|
||
function appendScript(id) { | ||
const script = document.createElement('script'); | ||
script.async = true; | ||
script.src = 'https://www.google-analytics.com/analytics.js'; | ||
script.src = 'https://www.googletagmanager.com/gtag/js?id=' + id; | ||
document.body.appendChild(script); | ||
} | ||
|
||
function init(id) { | ||
appendScript(); | ||
window.ga = | ||
window.ga || | ||
// global site tag instance initialized | ||
function initGlobalSiteTag(id) { | ||
appendScript(id); | ||
|
||
window.dataLayer = window.dataLayer || []; | ||
window.gtag = | ||
window.gtag || | ||
function () { | ||
(window.ga.q = window.ga.q || []).push(arguments); | ||
window.dataLayer.push(arguments); | ||
}; | ||
|
||
window.ga.l = Number(new Date()); | ||
window.ga('create', id, 'auto'); | ||
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.ga) { | ||
init($docsify.ga); | ||
} | ||
|
||
window.ga('set', 'page', location.hash); | ||
window.ga('send', 'pageview'); | ||
// usage: https://developers.google.com/analytics/devguides/collection/gtagjs/pages | ||
window.gtag('event', 'page_view', { | ||
page_title: document.title, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please disable eslint's "camelcase" rules for under_score properties to prevent lint warnings: window.gtag('event', 'page_view', {
/* eslint-disable camelcase */
page_title: document.title,
page_location: location.href,
page_path: location.pathname,
/* eslint-enable camelcase */
}); You can verify the fix by running There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, there is some questions:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we need to keep We should update the documentation and specify that the We should update the documentation to specify that the Perhaps our message under the
How does that sound? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just pushed the code, there is an exception in ci/codesandbox. I'm not sure what the reason is, please check it for me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took a look and tried a few things in #1851 but I was unable to find the issue. This happened once before with Codesandbox it is it kinda of a PITA to troublehoot because the failures don't happen locally or in CI tests. As much as I'd love to wrap this up, I don't have the spare cycles to devote to it right now. I will circle back as soon as I can devote some time to it. In the meantime, thanks for being patient with this, @ekoz. Very much appreciated. 😄 |
||
page_location: location.href, | ||
page_path: location.pathname, | ||
}); | ||
} | ||
|
||
const install = function (hook) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Modules, constants, and variables | ||
// npm run test:e2e ga.test.js | ||
// ----------------------------------------------------------------------------- | ||
const docsifyInit = require('../helpers/docsify-init'); | ||
const { test, expect } = require('./fixtures/docsify-init-fixture'); | ||
|
||
const gaTagList = [ | ||
'AW-YYYYYY', // Google Ads | ||
'DC-ZZZZZZ', // Floodlight | ||
'G-XXXXXX', // Google Analytics 4 (GA4) | ||
'UA-XXXXXX', // Google Universal Analytics (GA3) | ||
]; | ||
|
||
// Suite | ||
// ----------------------------------------------------------------------------- | ||
test.describe('GA 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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove or comment out the |
||
} | ||
}); | ||
} | ||
|
||
// Tests | ||
// --------------------------------------------------------------------------- | ||
test('single gtag', async ({ page }) => { | ||
pageRequestListened(page); | ||
|
||
const docsifyInitConfig = { | ||
config: { | ||
ga: gaTagList[0], | ||
}, | ||
scriptURLs: ['/lib/plugins/ga.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.ga, $docsify.ga === ''); | ||
|
||
// Tests | ||
expect($docsify.ga).not.toEqual(''); | ||
}); | ||
|
||
test('multi gtag', async ({ page }) => { | ||
pageRequestListened(page); | ||
|
||
const docsifyInitConfig = { | ||
config: { | ||
ga: gaTagList, | ||
}, | ||
scriptURLs: ['/lib/plugins/ga.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.ga, $docsify.ga === ''); | ||
|
||
// Tests | ||
expect($docsify.ga).not.toEqual(''); | ||
}); | ||
|
||
test('data-ga attribute', async ({ page }) => { | ||
pageRequestListened(page); | ||
|
||
// TODO | ||
}); | ||
}); |
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.
Let's move the
window.dataLayer
andwindow.gtag
setup outside of theinitGlobalSiteTag
function. This will allow users to callgtag()
so long as it is done after the plugin's<script>
tag:Plugin:
HTML:
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.
This is not work, I think we can ignore this function for the time being, and then pass the custom collector in through parameters when we need it later.