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

Fix: (third-parties) sendGTMEvent not queueing events before GTM init (#68683) #72111

Merged
merged 2 commits into from
Nov 13, 2024

Conversation

ijjk
Copy link
Member

@ijjk ijjk commented Oct 30, 2024

Backports: #68683

…#68683)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md




## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

sendGTMEvent pushes items to window.datalayer. THe GoogleTagManger (GTM)
client uses this to know what events to the GTM server.

currently if GTM is not ready, instead of adding events to the
dataLayer, it writes a warning to the console saying GTM is not ready.

The PR fixes that by defining the dataLayer if it is undefined so it can
start to queue up events before GTM is ready

### Why?

If GTM init is slow we don't want to loose any events that we want to
send before GTM init has happened we want to queue them up in the
dataLayer



### How?

The PR fixes that by defining the dataLayer if it is undefined so it can
start to queue up events before GTM is ready

Closes NEXT-
Fixes #

fixes #66865 (discussion)
fixes #68678 (issue)

-->

### What?

sendGTMEvent pushes items to window.datalayer. THe GoogleTagManger (GTM)
client uses this to know what events to the GTM server.

currently if GTM is not ready, instead of adding events to the
dataLayer, it writes a warning to the console saying GTM is not ready.

The PR fixes that by defining the dataLayer if it is undefined so it can
start to queue up events before GTM is ready


https://developers.google.com/tag-platform/tag-manager/datalayer#how_data_layer_information_is_processed

### Why?

If GTM init is slow we don't want to loose any events that we want to
send before GTM init has happened we want to queue them up in the
dataLayer

Another use case for this behaviour, is that you can optionally
initialise GoogleTagManager based on environment so you don't pollute
GTM data when developing or running playright tests, but you can still
evaluate what exists on the dataLayer to test if the correct events
would be sent to GTM after testing interactions with the app.

### How?

The PR fixes that by defining the dataLayer if it is undefined so it can
start to queue up events before GTM is ready

Fixes #

fixes #66865 (discussion)
fixes #68678 (issue)

---------

Co-authored-by: JJ Kasper <[email protected]>
@ijjk ijjk added the created-by: Next.js team PRs by the Next.js team. label Oct 30, 2024
@ijjk ijjk requested a review from ztanner October 30, 2024 22:14
@ijjk
Copy link
Member Author

ijjk commented Oct 30, 2024

Stats from current PR

Default Build (Increase detected ⚠️)
General
vercel/next.js 14-2-1 vercel/next.js ijjk/backport-third-parties-fix Change
buildDuration 18.4s 15.9s N/A
buildDurationCached 9.4s 7.4s N/A
nodeModulesSize 200 MB 200 MB
nextStartRea..uration (ms) 421ms 398ms N/A
Client Bundles (main, webpack)
vercel/next.js 14-2-1 vercel/next.js ijjk/backport-third-parties-fix Change
1a9f679d-HASH.js gzip 53.7 kB 53.7 kB N/A
5428.HASH.js gzip 181 B 180 B N/A
6067-HASH.js gzip 5.06 kB 5.06 kB N/A
6428-HASH.js gzip 31.6 kB 31.6 kB N/A
framework-HASH.js gzip 44.9 kB 44.9 kB
main-app-HASH.js gzip 242 B 243 B N/A
main-HASH.js gzip 32.3 kB 32.3 kB N/A
webpack-HASH.js gzip 1.68 kB 1.68 kB N/A
Overall change 44.9 kB 44.9 kB
Legacy Client Bundles (polyfills)
vercel/next.js 14-2-1 vercel/next.js ijjk/backport-third-parties-fix Change
polyfills-HASH.js gzip 39.4 kB 39.4 kB
Overall change 39.4 kB 39.4 kB
Client Pages
vercel/next.js 14-2-1 vercel/next.js ijjk/backport-third-parties-fix Change
_app-HASH.js gzip 196 B 196 B
_error-HASH.js gzip 184 B 185 B N/A
amp-HASH.js gzip 502 B 504 B N/A
css-HASH.js gzip 321 B 324 B N/A
dynamic-HASH.js gzip 1.82 kB 1.82 kB N/A
edge-ssr-HASH.js gzip 258 B 257 B N/A
head-HASH.js gzip 352 B 352 B
hooks-HASH.js gzip 371 B 372 B N/A
image-HASH.js gzip 4.23 kB 4.23 kB N/A
index-HASH.js gzip 259 B 257 B N/A
link-HASH.js gzip 2.67 kB 2.68 kB N/A
routerDirect..HASH.js gzip 316 B 314 B N/A
script-HASH.js gzip 385 B 386 B N/A
withRouter-HASH.js gzip 311 B 310 B N/A
1afbb74e6ecf..834.css gzip 106 B 106 B
Overall change 654 B 654 B
Client Build Manifests
vercel/next.js 14-2-1 vercel/next.js ijjk/backport-third-parties-fix Change
_buildManifest.js gzip 483 B 481 B N/A
Overall change 0 B 0 B
Rendered Page Sizes
vercel/next.js 14-2-1 vercel/next.js ijjk/backport-third-parties-fix Change
index.html gzip 529 B 527 B N/A
link.html gzip 542 B 539 B N/A
withRouter.html gzip 525 B 523 B N/A
Overall change 0 B 0 B
Edge SSR bundle Size
vercel/next.js 14-2-1 vercel/next.js ijjk/backport-third-parties-fix Change
edge-ssr.js gzip 95.5 kB 95.5 kB N/A
page.js gzip 3.06 kB 3.06 kB N/A
Overall change 0 B 0 B
Middleware size
vercel/next.js 14-2-1 vercel/next.js ijjk/backport-third-parties-fix Change
middleware-b..fest.js gzip 657 B 656 B N/A
middleware-r..fest.js gzip 156 B 154 B N/A
middleware.js gzip 25.5 kB 25.5 kB N/A
edge-runtime..pack.js gzip 839 B 839 B
Overall change 839 B 839 B
Next Runtimes
vercel/next.js 14-2-1 vercel/next.js ijjk/backport-third-parties-fix Change
app-page-exp...dev.js gzip 171 kB 171 kB
app-page-exp..prod.js gzip 98.3 kB 98.3 kB
app-page-tur..prod.js gzip 100 kB 100 kB
app-page-tur..prod.js gzip 94.3 kB 94.3 kB
app-page.run...dev.js gzip 145 kB 145 kB
app-page.run..prod.js gzip 92.8 kB 92.8 kB
app-route-ex...dev.js gzip 22.1 kB 22.1 kB
app-route-ex..prod.js gzip 15.5 kB 15.5 kB
app-route-tu..prod.js gzip 15.5 kB 15.5 kB
app-route-tu..prod.js gzip 15.2 kB 15.2 kB
app-route.ru...dev.js gzip 21.7 kB 21.7 kB
app-route.ru..prod.js gzip 15.2 kB 15.2 kB
pages-api-tu..prod.js gzip 9.58 kB 9.58 kB
pages-api.ru...dev.js gzip 9.85 kB 9.85 kB
pages-api.ru..prod.js gzip 9.57 kB 9.57 kB
pages-turbo...prod.js gzip 22.5 kB 22.5 kB
pages.runtim...dev.js gzip 23.2 kB 23.2 kB
pages.runtim..prod.js gzip 22.5 kB 22.5 kB
server.runti..prod.js gzip 51.6 kB 51.6 kB
Overall change 956 kB 956 kB
build cache Overall increase ⚠️
vercel/next.js 14-2-1 vercel/next.js ijjk/backport-third-parties-fix Change
0.pack gzip 1.6 MB 1.61 MB ⚠️ +2.94 kB
index.pack gzip 114 kB 114 kB ⚠️ +155 B
Overall change 1.72 MB 1.72 MB ⚠️ +3.1 kB
Diff details
Diff for middleware.js

Diff too large to display

Diff for edge-ssr.js

Diff too large to display

Commit: f133aac

@ijjk ijjk closed this Nov 5, 2024
@ijjk ijjk deleted the ijjk/backport-third-parties-fix branch November 5, 2024 23:30
@ijjk ijjk restored the ijjk/backport-third-parties-fix branch November 13, 2024 20:04
@ijjk ijjk reopened this Nov 13, 2024
@ijjk ijjk merged commit 515055a into 14-2-1 Nov 13, 2024
49 of 55 checks passed
@ijjk ijjk deleted the ijjk/backport-third-parties-fix branch November 13, 2024 20:18
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
created-by: Next.js team PRs by the Next.js team. locked
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants