mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 00:27:21 +02:00
fix(v2): make Google analytics and Google gtag plugins work
This commit is contained in:
parent
cfffad8c6d
commit
8a6f8ef9c3
3 changed files with 11 additions and 7 deletions
|
@ -24,7 +24,7 @@ export default (function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
// process.env.NODE_ENV !== 'production' || // TODO: Add it back after testing that it works.
|
process.env.NODE_ENV !== 'production' ||
|
||||||
!trackingID ||
|
!trackingID ||
|
||||||
typeof window === 'undefined'
|
typeof window === 'undefined'
|
||||||
) {
|
) {
|
||||||
|
|
|
@ -24,32 +24,36 @@ export default (function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
// process.env.NODE_ENV !== 'production' || // TODO: Add it back after testing that it works.
|
process.env.NODE_ENV !== 'production' ||
|
||||||
!trackingID ||
|
!trackingID ||
|
||||||
typeof window === 'undefined'
|
typeof window === 'undefined'
|
||||||
) {
|
) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* eslint-disable */
|
||||||
const $scriptEl = window.document.createElement('script');
|
const $scriptEl = window.document.createElement('script');
|
||||||
$scriptEl.async = 1;
|
$scriptEl.async = 1;
|
||||||
$scriptEl.src = `https://www.googletagmanager.com/gtag/js?id=${trackingID}`;
|
$scriptEl.src = `https://www.googletagmanager.com/gtag/js?id=${trackingID}`;
|
||||||
window.document.head.appendChild($scriptEl);
|
window.document.head.appendChild($scriptEl);
|
||||||
|
|
||||||
window.dataLayer = window.dataLayer || [];
|
window.dataLayer = window.dataLayer || [];
|
||||||
// eslint-disable-next-line no-inner-declarations
|
function gtag() {
|
||||||
function gtag(...args) {
|
// Have to use `arguments` instead of spreading as there are
|
||||||
window.dataLayer.push(args);
|
// other properties attached to it e.g. callee.
|
||||||
|
// The GA library requires usage of `arguments.
|
||||||
|
window.dataLayer.push(arguments);
|
||||||
}
|
}
|
||||||
// Expose globally.
|
// Expose globally.
|
||||||
window.gtag = gtag;
|
window.gtag = gtag;
|
||||||
gtag('js', new Date());
|
gtag('js', new Date());
|
||||||
gtag('config', trackingID);
|
gtag('config', trackingID);
|
||||||
|
/* eslint-enable */
|
||||||
|
|
||||||
return {
|
return {
|
||||||
onRouteUpdate({location}) {
|
onRouteUpdate({location}) {
|
||||||
// Always refer to the variable on window in-case it gets overridden elsewhere.
|
// Always refer to the variable on window in-case it gets overridden elsewhere.
|
||||||
window.gtag('event', 'page_view', {
|
window.gtag('config', trackingID, {
|
||||||
page_path: location.pathname,
|
page_path: location.pathname,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -3,4 +3,4 @@ id: analytics
|
||||||
title: Adding Analytics
|
title: Adding Analytics
|
||||||
---
|
---
|
||||||
|
|
||||||
TODO: Talk about how to add Algolia search and offline search via the search plugin (TBD).
|
TODO: Talk about using Google analytics and Google gtag plugins and how to track using your own plugins.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue