refactor(plugin-gtag): update gtag plugin to modern SPA recommendations (#8143)

Co-authored-by: Lane Goolsby <lanegoolsby@rocketmortgage.com>
Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
Lane Goolsby 2022-10-21 05:59:12 -07:00 committed by GitHub
parent 5ddcbf47a8
commit e41133245c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 17 deletions

View File

@ -21,6 +21,7 @@
"@docusaurus/core": "^3.0.0-alpha.0",
"@docusaurus/types": "^3.0.0-alpha.0",
"@docusaurus/utils-validation": "^3.0.0-alpha.0",
"@types/gtag.js": "^0.0.12",
"tslib": "^2.4.0"
},
"peerDependencies": {

View File

@ -23,11 +23,12 @@ const clientModule: ClientModule = {
setTimeout(() => {
// Always refer to the variable on window in case it gets overridden
// elsewhere.
window.gtag('event', 'page_view', {
page_title: document.title,
page_location: window.location.href,
page_path: location.pathname + location.search + location.hash,
});
window.gtag(
'set',
'page_path',
location.pathname + location.search + location.hash,
);
window.gtag('event', 'page_view');
});
}
},

View File

@ -6,15 +6,3 @@
*/
/// <reference types="@docusaurus/module-type-aliases" />
interface Window {
gtag: (
command: string,
fields: string,
params: {
page_title?: string;
page_location?: string;
page_path?: string;
},
) => void;
}

View File

@ -0,0 +1,36 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import Layout from '@theme/Layout';
// See https://github.com/facebook/docusaurus/issues/6337#issuecomment-1012913647
export default function Analytics(): JSX.Element {
return (
<Layout>
<h1>Test Analytics</h1>
<div>
<button
type="button"
onClick={() => {
window.gtag('event', 'docusaurus-test-event', {
event_category: 'docusaurus-test-event-category"',
event_label: 'docusaurus-test-event-label',
});
const FALSE = false; // I don't want gtag to run with bad params
if (FALSE) {
// @ts-expect-error: gtag usage is type safe, failure expected
window.gtag('this-does-not-exist');
}
}}>
Submit custom gtag event!
</button>
</div>
</Layout>
);
}

View File

@ -31,3 +31,4 @@ import Readme from "../README.md"
- [Tabs tests](/tests/pages/tabs-tests)
- [z-index tests](/tests/pages/z-index-tests)
- [Head metadata tests](/tests/pages/head-metadata)
- [Analytics](/tests/pages/analytics)

View File

@ -6,3 +6,4 @@
*/
/// <reference types="@docusaurus/plugin-ideal-image" />
/// <reference types="@types/gtag.js" />

View File

@ -3529,6 +3529,11 @@
dependencies:
"@types/node" "*"
"@types/gtag.js@^0.0.12":
version "0.0.12"
resolved "https://registry.yarnpkg.com/@types/gtag.js/-/gtag.js-0.0.12.tgz#095122edca896689bdfcdd73b057e23064d23572"
integrity sha512-YQV9bUsemkzG81Ea295/nF/5GijnD2Af7QhEofh7xu+kvCN6RdodgNwwGWXB5GMI3NoyvQo0odNctoH/qLMIpg==
"@types/hast@^2.0.0":
version "2.3.4"
resolved "https://registry.yarnpkg.com/@types/hast/-/hast-2.3.4.tgz#8aa5ef92c117d20d974a82bdfb6a648b08c0bafc"