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(gatsby-plugin-google-analytics): Add pageTransitionDelay option #15610

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/gatsby-plugin-google-analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ module.exports = {
respectDNT: true,
// Avoids sending pageview hits from custom paths
exclude: ["/preview/**", "/do-not-track/me/too/"],
// Delays sending pageview hits on route update (in milliseconds)
pageTransitionDelay: 0,
// Enables Google Optimize using your container Id
optimizeId: "YOUR_GOOGLE_OPTIMIZE_TRACKING_ID",
// Enables Google Optimize Experiment ID
Expand Down Expand Up @@ -102,6 +104,10 @@ If you enable this optional option, Google Analytics will not be loaded at all f

If you need to exclude any path from the tracking system, you can add it (one or more) to this optional array as glob expressions.

### `pageTransitionDelay`

If your site uses any custom transitions on route update (e.g. [`gatsby-plugin-transition-link`](https://www.gatsbyjs.org/blog/2018-12-04-per-link-gatsby-page-transitions-with-transitionlink/)), then you can delay processing the page view event until the new page is mounted.

### `optimizeId`

If you need to use Google Optimize for A/B testing, you can add this optional Optimize container id to allow Google Optimize to load the correct test parameters for your site.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { onRouteUpdate } from "../gatsby-browser"
import { Minimatch } from "minimatch"

jest.useFakeTimers()

describe(`gatsby-plugin-google-analytics`, () => {
describe(`gatsby-browser`, () => {
describe(`onRouteUpdate`, () => {
Expand All @@ -29,12 +27,17 @@ describe(`gatsby-plugin-google-analytics`, () => {
})

beforeEach(() => {
jest.useFakeTimers()
window.ga = jest.fn()
window.requestAnimationFrame = jest.fn(cb => {
Simon-Tang marked this conversation as resolved.
Show resolved Hide resolved
cb()
})
})

afterEach(() => {
jest.resetAllMocks()
})

it(`does not send page view when ga is undefined`, () => {
delete window.ga

Expand Down Expand Up @@ -72,6 +75,15 @@ describe(`gatsby-plugin-google-analytics`, () => {
expect(setTimeout).toHaveBeenCalledTimes(1)
expect(window.ga).toHaveBeenCalledTimes(2)
})

it(`uses setTimeout when pageTransitionDelay is set`, () => {
onRouteUpdate({}, { pageTransitionDelay: 1000 })

jest.runAllTimers()

expect(setTimeout).toHaveBeenCalledTimes(1)
Simon-Tang marked this conversation as resolved.
Show resolved Hide resolved
expect(window.ga).toHaveBeenCalledTimes(2)
})
})
})
})
Expand Down
11 changes: 6 additions & 5 deletions packages/gatsby-plugin-google-analytics/src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const onRouteUpdate = ({ location }) => {
export const onRouteUpdate = ({ location }, pluginOptions = {}) => {
if (process.env.NODE_ENV !== `production` || typeof ga !== `function`) {
return null
}
Expand All @@ -20,13 +20,14 @@ export const onRouteUpdate = ({ location }) => {
window.ga(`send`, `pageview`)
}

if (`requestAnimationFrame` in window) {
const delay = pluginOptions.pageTransitionDelay
Simon-Tang marked this conversation as resolved.
Show resolved Hide resolved
if (delay || !(`requestAnimationFrame` in window)) {
// minimum delay to simulate a pair of requestAnimationFrame calls
setTimeout(sendPageView, Math.min(32, delay))
} else {
requestAnimationFrame(() => {
requestAnimationFrame(sendPageView)
})
} else {
// simulate 2 rAF calls
setTimeout(sendPageView, 32)
}

return null
Expand Down