-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
[1.0] Fixes 1317 Google Analytics plugin; updates attachHistory listener logic #1318
[1.0] Fixes 1317 Google Analytics plugin; updates attachHistory listener logic #1318
Conversation
…preventing duplicate counting of pageviews
// Don't track while developing. | ||
if (process.env.NODE_ENV === `production`) { | ||
ga(`set`, `page`, location.pathname) | ||
ga(`send`, `pageview`) | ||
var newPath = newRoute.pathname || newRoute.location.pathname |
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.
@KyleAMathews this newPath
lastPath
thing can be solved somewhere else probably, if you can point me in the right direction!
Essentially, as we navigate through a site, the onRouteUpdate
function gets called one additional time, like:
Home page - fires once
... user goes to about page - fires twice
... user goes back to homepage - fires three times.
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.
Ugh, yeah this code is the cause :-(
history.listen((location, action) => { |
Try changing that function so that it only attaches once.
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.
For the code here, you should do:
exports.onRouteUpdate = function({ location }) {
Deploy preview ready! Built with commit ddccb5c |
Deploy preview ready! Built with commit ddccb5c |
Deploy preview ready! Built with commit ddccb5c |
@@ -0,0 +1,19 @@ | |||
# editorconfig.org |
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.
ah nice!
@@ -1,7 +1,7 @@ | |||
exports.onRouteUpdate = function(location) { | |||
exports.onRouteUpdate = function({ location }) { |
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.
Added destructuring, I guess I misread the plugin docs regarding the src
folder.
Deploy preview failed. Built with commit b4e2c8c https://app.netlify.com/sites/using-glamor/deploys/5956c80a7960b164cee09a62 |
history.listen((location, action) => { | ||
apiRunner(`onRouteUpdate`, { location, action }) | ||
}) | ||
} |
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.
Updated, also noticed that this same code exists within:
gatsby/packages/gatsby/src/cache-dir/root.js
Line 104 in 08bddd9
attachToHistory(routeProps.history) |
But the production-app.js file is the root cause
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.
yeah, root.js is for the development version of Gatsby. It should be fixed too but since you're not generally tracking yourself in development it's not as urgent.
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.
well I went ahead and adjusted it too!
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.
Perfect :-)
Thanks! Pulling this in now!
Hiya @camsjams! 👋 This is definitely late, but on behalf of the entire Gatsby community, I wanted to say thank you for being here. Gatsby is built by awesome people like you. Let us say “thanks” in two ways:
If you have questions, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’. Thanks again! 💪💜 |
@jlengstorf thanks! I would love to be part of the maintainers team - I use Gatsby on several large production sites and static WordPress blogs. |
Fixes #1317
GA metrics shows whatever the first SSR-served landing page is because page will never change due to an
undefined
reference.Also found a linearly growing page view bug where each call to onRouteUpdate is called more than one time. Added the "solution" to outline the problem but wanted to chat about it since I know we can probably see about preventing the handler from being attached more than once.
Also adds: