Skip to content

Commit

Permalink
fix analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Apr 29, 2022
1 parent 9286297 commit c244bb1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
20 changes: 9 additions & 11 deletions packages/docusaurus-plugin-google-analytics/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
* LICENSE file in the root directory of this source tree.
*/

import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import type {ClientModule} from '@docusaurus/types';

export default (function analyticsModule() {
if (!ExecutionEnvironment.canUseDOM) {
return null;
}

return {
onRouteDidUpdate({location}: {location: Location}) {
const clientModule: ClientModule = {
onRouteDidUpdate({location, previousLocation}) {
if (previousLocation && location.pathname !== previousLocation.pathname) {
// 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);
// Always refer to the variable on window in-case it gets
// overridden elsewhere.
window.ga('send', 'pageview');
},
};
})();
}
},
};

export default clientModule;
26 changes: 12 additions & 14 deletions packages/docusaurus-plugin-google-gtag/src/gtag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@
* LICENSE file in the root directory of this source tree.
*/

import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import globalData from '@generated/globalData';
import type {PluginOptions} from '@docusaurus/plugin-google-gtag';
import type {ClientModule} from '@docusaurus/types';

export default (function gtagModule() {
if (!ExecutionEnvironment.canUseDOM) {
return null;
}
const {trackingID} = globalData['docusaurus-plugin-google-gtag']!
.default as PluginOptions;

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

return {
onRouteDidUpdate({location}: {location: Location}) {
const clientModule: ClientModule = {
onRouteDidUpdate({location, previousLocation}) {
if (previousLocation && location.pathname !== previousLocation.pathname) {
// Always refer to the variable on window in case it gets overridden
// elsewhere.
window.gtag('config', trackingID, {
Expand All @@ -27,9 +23,11 @@ export default (function gtagModule() {
});
window.gtag('event', 'page_view', {
page_title: document.title,
page_location: location.href,
page_location: window.location.href,
page_path: location.pathname,
});
},
};
})();
}
},
};

export default clientModule;

0 comments on commit c244bb1

Please sign in to comment.