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

Adding <GoogleTagManager> component to @next/third-parties #56106

Merged
merged 31 commits into from
Oct 17, 2023

Conversation

janicklas-ralph
Copy link
Contributor

@janicklas-ralph janicklas-ralph commented Sep 27, 2023

This PR adds the <GoogleTagManager> component along with the sendGTMEvent helper to @next/third-parties repo.

@ijjk ijjk added created-by: Chrome Aurora PRs by the Google Chrome team: https://web.dev/aurora type: next labels Sep 27, 2023
@sanjaiyan-dev
Copy link
Contributor

Just saw a minor typo in the heading 🫣😅

useGoogleTagManager instead of useGoogltTagManager 🫱🏼‍🫲🏾

@janicklas-ralph janicklas-ralph changed the title Adding useGoogltTagManager hook to @next/third-parties Adding useGoogleTagManager hook to @next/third-parties Sep 27, 2023
@janicklas-ralph janicklas-ralph marked this pull request as ready for review September 27, 2023 20:51
Copy link
Collaborator

@housseindjirdeh housseindjirdeh left a comment

Choose a reason for hiding this comment

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

Mostly higher-level questions that can mostly be addressed in future PRs. Otherwise, this LGTM.

@@ -1,2 +1,3 @@
export { default as GoogleMapsEmbed } from './GoogleMapsEmbed'
export { default as YouTubeEmbed } from './YouTubeEmbed'
export { default as useGoogleTagManager } from './useGoogleTagManager'
Copy link
Collaborator

Choose a reason for hiding this comment

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

@janicklas-ralph Do you know if the bundle is being tree-shaken with this approach? For example, I import useGoogleTagManager in my app, is GoogleMapsEmbed and YouTubeEmbed included into the total bundle even if they're not used?

packages/third-parties/src/google/index.tsx Outdated Show resolved Hide resolved
)
}

export default Page
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can do this in a separate PR, but we should update the GTM example in the examples/ repo to show this newer approach

Copy link

Choose a reason for hiding this comment

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

Is the double <GoogleTagManager gtmId="GTM-XYZ" /> on line 13 and 18 an oversight? Same in test/e2e/third-parties/pages/gtm.js

Copy link
Collaborator

Choose a reason for hiding this comment

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

I had thought the same in the beginning, but I recall @janicklas-ralph mentioning that it is was deliberate to test that the behavior if an identical GTM containers is included by accident

@janicklas-ralph Can you clarify, and if so, add a note here in a future PR? (not urgent)

@ijjk
Copy link
Member

ijjk commented Sep 27, 2023

Stats from current PR

Default Build
General
vercel/next.js canary janicklas-ralph/next.js gtm Change
buildDuration 10.8s 10.8s N/A
buildDurationCached 6.4s 6.3s N/A
nodeModulesSize 174 MB 174 MB
nextStartRea..uration (ms) 537ms 515ms N/A
Client Bundles (main, webpack)
vercel/next.js canary janicklas-ralph/next.js gtm Change
199-HASH.js gzip 27.5 kB 27.5 kB
3f784ff6-HASH.js gzip 53.1 kB 53.1 kB
99.HASH.js gzip 182 B 182 B
framework-HASH.js gzip 45.3 kB 45.3 kB
main-app-HASH.js gzip 254 B 252 B N/A
main-HASH.js gzip 32.9 kB 32.9 kB N/A
webpack-HASH.js gzip 1.75 kB 1.75 kB N/A
Overall change 126 kB 126 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary janicklas-ralph/next.js gtm Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary janicklas-ralph/next.js gtm Change
_app-HASH.js gzip 206 B 205 B N/A
_error-HASH.js gzip 182 B 180 B N/A
amp-HASH.js gzip 506 B 505 B N/A
css-HASH.js gzip 322 B 323 B N/A
dynamic-HASH.js gzip 2.57 kB 2.57 kB N/A
edge-ssr-HASH.js gzip 260 B 259 B N/A
head-HASH.js gzip 350 B 350 B
hooks-HASH.js gzip 369 B 369 B
image-HASH.js gzip 4.35 kB 4.35 kB N/A
index-HASH.js gzip 256 B 256 B
link-HASH.js gzip 2.64 kB 2.63 kB N/A
routerDirect..HASH.js gzip 312 B 311 B N/A
script-HASH.js gzip 385 B 384 B N/A
withRouter-HASH.js gzip 307 B 308 B N/A
1afbb74e6ecf..834.css gzip 106 B 106 B
Overall change 1.08 kB 1.08 kB
Client Build Manifests
vercel/next.js canary janicklas-ralph/next.js gtm Change
_buildManifest.js gzip 485 B 482 B N/A
Overall change 0 B 0 B
Rendered Page Sizes
vercel/next.js canary janicklas-ralph/next.js gtm Change
index.html gzip 528 B 530 B N/A
link.html gzip 541 B 542 B N/A
withRouter.html gzip 523 B 524 B N/A
Overall change 0 B 0 B
Edge SSR bundle Size
vercel/next.js canary janicklas-ralph/next.js gtm Change
edge-ssr.js gzip 93.7 kB 93.7 kB N/A
page.js gzip 154 kB 154 kB N/A
Overall change 0 B 0 B
Middleware size
vercel/next.js canary janicklas-ralph/next.js gtm Change
middleware-b..fest.js gzip 625 B 621 B N/A
middleware-r..fest.js gzip 150 B 151 B N/A
middleware.js gzip 22.5 kB 22.5 kB N/A
edge-runtime..pack.js gzip 1.92 kB 1.92 kB
Overall change 1.92 kB 1.92 kB
Commit: 0ecadb9

Copy link
Collaborator

@housseindjirdeh housseindjirdeh left a comment

Choose a reason for hiding this comment

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

Just one nit re: naming. Otherwise LGTM

packages/third-parties/src/google/gtm.tsx Outdated Show resolved Hide resolved
let currDataLayerName = 'dataLayer'

export function GoogleTagManager(props: GTMParams) {
const { gtmId, dataLayerName = 'dataLayer', auth, preview, dataLayer } = props
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we rename gtmId to just id?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I originally had it as id but since I made it into a component, I am accepting the GTM params as props

<GoogleTagManager gtmId="GTM_XYZ" />

In this case id might clash with HTML id so I changed it to gtmID

packages/third-parties/src/google/gtm.tsx Outdated Show resolved Hide resolved
packages/third-parties/src/google/gtm.tsx Outdated Show resolved Hide resolved
packages/third-parties/src/google/index.tsx Outdated Show resolved Hide resolved
export function GoogleTagManager(props: GTMParams) {
const { gtmId, dataLayerName = 'dataLayer', auth, preview, dataLayer } = props

currDataLayerName = dataLayerName
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this pattern is edge-case free. When you render this component it will use the initial dataLayer prop to instantiate the script by id. Then afterwards any changes to the dataLayer prop will not be reflected in teh gtm script but will alter this module scoped variable.

The sendGTMEvent refers to the current dataLayer rather than the the one initially used instantiate the script so a wayward props update could breat gtm tracking.

If it is true that you can't hot-update the datalayer in GTM (meaning you need to just pick it at the start of the app runtime and never change it) then we should align the module global to only update when the gtm script will actually run which is just once one hte first render.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense! I have updated the code to only modify currDataLayerName during initialization

Copy link
Contributor

@gnoff gnoff left a comment

Choose a reason for hiding this comment

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

I'm made a suggestion for a dev only warning but the implementation looks good. Let's rename the PR though to reflect the actual API now since it is a component paired with a function and not just a hook.

Comment on lines +26 to +28
if (currDataLayerName === undefined) {
currDataLayerName = dataLayerName
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding a warning in dev if you later update the layer name to something else. Since you cannot modify the layer name after initialization this is erroneous but since the default layer continues to work I'd just make it a console error since it'll just work with the initial layer in production

@kodiakhq kodiakhq bot merged commit 5e474a3 into vercel:canary Oct 17, 2023
53 of 58 checks passed
@janicklas-ralph janicklas-ralph changed the title Adding useGoogleTagManager hook to @next/third-parties Adding <GoogleTagManager> component to @next/third-parties Oct 17, 2023
@github-actions github-actions bot added the locked label Nov 8, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
created-by: Chrome Aurora PRs by the Google Chrome team: https://web.dev/aurora locked type: next
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants