Skip to content
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

feat(analytics): allow query/hash changes to be sent to GA #7545

Merged
merged 1 commit into from Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/docusaurus-plugin-google-analytics/src/analytics.ts
Expand Up @@ -9,10 +9,19 @@ import type {ClientModule} from '@docusaurus/types';

const clientModule: ClientModule = {
onRouteDidUpdate({location, previousLocation}) {
if (previousLocation && location.pathname !== previousLocation.pathname) {
if (
previousLocation &&
(location.pathname !== previousLocation.pathname ||
location.search !== previousLocation.search ||
location.hash !== previousLocation.hash)
) {
// Set page so that subsequent hits on this page are attributed
// to this page. This is recommended for Single-page Applications.
window.ga('set', 'page', location.pathname);
window.ga(
'set',
'page',
location.pathname + location.search + location.hash,
);
// Always refer to the variable on window in-case it gets
// overridden elsewhere.
window.ga('send', 'pageview');
Expand Down
18 changes: 7 additions & 11 deletions packages/docusaurus-plugin-google-gtag/src/gtag.ts
Expand Up @@ -5,16 +5,16 @@
* LICENSE file in the root directory of this source tree.
*/

import globalData from '@generated/globalData';
import type {PluginOptions} from './options';
import type {ClientModule} from '@docusaurus/types';

const {trackingID} = globalData['docusaurus-plugin-google-gtag']!
.default as PluginOptions;

const clientModule: ClientModule = {
onRouteDidUpdate({location, previousLocation}) {
if (previousLocation && location.pathname !== previousLocation.pathname) {
if (
previousLocation &&
(location.pathname !== previousLocation.pathname ||
location.search !== previousLocation.search ||
location.hash !== previousLocation.hash)
) {
// Normally, the document title is updated in the next tick due to how
// `react-helmet-async` updates it. We want to send the current document's
// title to gtag instead of the old one's, so we use `setTimeout` to defer
Expand All @@ -23,14 +23,10 @@ const clientModule: ClientModule = {
setTimeout(() => {
// Always refer to the variable on window in case it gets overridden
// elsewhere.
window.gtag('config', trackingID, {
page_path: location.pathname,
page_title: document.title,
});
Comment on lines -26 to -29
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config is done here so doing it again here is redundant.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know the practice here, because here we are also sending page_path etc. Do we have docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the reference I used to verify. I also poked around in some internal code in our org and verified that config does not need to be ran each time. Also, near as I can tell, the page_path and page_title are ignored if passed in as a config object. config is mostly intended for handling multiple property tracker IDs, campaign IDs, etc. Its not intended as a way to log page events.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see—sounds good to me, then!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually like a bug fix to me and also something I suspected

When navigating, there were 2 network entries:

CleanShot 2022-06-02 at 12 41 49@2x

Now there's only one 👍

window.gtag('event', 'page_view', {
page_title: document.title,
page_location: window.location.href,
page_path: location.pathname,
page_path: location.pathname + location.search + location.hash,
});
});
}
Expand Down