Skip to content

Conversation

vijayraghav-io
Copy link
Contributor

@vijayraghav-io vijayraghav-io commented May 23, 2025

What does this PR do?

Root cause
Layout - apps/web/app/(use-page-wrapper)/layout.tsx where global GTM container scripts are injected through env.
This layout also wraps the apps/web/app/(use-page-wrapper)/d/[link]/[slug]/page.tsx (private booking page)
Hence the gtm.js is loaded for private booking page.

Implemented solution
Move the d/[link]/[slug]/page.tsx to under (booking-page-wrapper)

Alternate solution
Apply a route/path based filter in (use-page-wrapper)/layout.tsx , to not inject scripts for /d/[link]/[slug]/page.tsx
For this the exact pathname has to be detected.
h.get('Referer) will not work all the time.
So we need to append a header like x-pathname in middleware. This requires to allow all paths to middleware.
We can create a parallel middleware without modifying existing middleware.
The new middleware allows all paths and only appends the x-pathname from req.nextUrl.pathname.
The performance implications of all requests or paths reaching middleware has to be considered.

Visual Demo (For contributors especially)

Video Demo (if applicable):

Before Changes - https://www.loom.com/share/93edfb9104fb445bb329946631e7df87?sid=95f1ce78-7133-43f8-8b7c-98fa9b74a9a8

After Changes - https://www.loom.com/share/a89aefbf16f94ecb8cf17be798a7515c?sid=a0ef0fc8-8cce-4e70-8a35-38e50d9f53f9

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • -N/A I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

  • Are there environment variables that should be set?
  • What are the minimal test data to have?
    In .env
    Set NEXT_PUBLIC_BODY_SCRIPTS=(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','GTM-PV22BDLZ');
  • What is expected (happy path) to have (input and output)?
    Create a private booking link for any event. (Edit -> EventType -> Advanced Tab -> Private links)
    Visit this link, gtm.js should not be loaded when checked in network tab
    Also check the script with id = injected-body-script should not be loaded inside the html body
    Visit any other page (except booking pages), the gtm.js should be loaded.
  • Any other important info that could help to test that PR

Summary by cubic

Stopped the Google Tag Manager script from loading on private booking links to protect user privacy.

  • Bug Fixes
    • Added a route filter in the layout to prevent GTM scripts from injecting on /d/[link]/[slug] pages.

@vijayraghav-io vijayraghav-io requested review from a team as code owners May 23, 2025 13:29
@graphite-app graphite-app bot added the community Created by Linear-GitHub Sync label May 23, 2025
@github-actions github-actions bot added booking-page area: booking page, public booking page, booker 🐛 bug Something isn't working labels May 23, 2025
Copy link

vercel bot commented May 23, 2025

@vijayraghav-io is attempting to deploy a commit to the cal Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

graphite-app bot commented May 23, 2025

Graphite Automations

"Add community label" took an action on this PR • (05/23/25)

1 label was added to this PR based on Keith Williams's automation.

"Add ready-for-e2e label" took an action on this PR • (05/25/25)

1 label was added to this PR based on Keith Williams's automation.

@vijayraghav-io
Copy link
Contributor Author

vijayraghav-io commented May 23, 2025

@hariombalhara , Kindly review.
Moved d/[link]/[slug]/page.tsx to under (booking-page-wrapper).
Please refer description for more details and alternate approach 🙏

@vijayraghav-io vijayraghav-io changed the title fix: GTM container on private booking links fix: Remove global GTM container on private booking links. May 24, 2025
@vijayraghav-io vijayraghav-io changed the title fix: Remove global GTM container on private booking links. fix: Remove global GTM container on private booking page. May 24, 2025
@vijayraghav-io
Copy link
Contributor Author

vijayraghav-io commented May 24, 2025

Allowing catch all paths in existing middleware requires to whitelist certain paths.
Considering the recent changes in middleware to remove catch all for performance, dint want to bring back this.

So, switched to other method of moving d/[link]/[slug]/page.tsx to under (booking-page-wrapper) from (use-page-wrapper)

@vijayraghav-io
Copy link
Contributor Author

vijayraghav-io commented May 24, 2025

Added e2e tests :

test - "global GTM should not be loaded on private booking link" - verifies the script with id=injected-body-script is Not loaded on visiting private booking page link.

test - "global GTM should be loaded on non-booking pages" - verifies that the current PR changes will not affected the GTM script being loaded on Non-booking pages. Given the script is injected through env.

Verified both tests Passing locally.

@vijayraghav-io
Copy link
Contributor Author

vijayraghav-io commented May 24, 2025

To more verify the fix :
when test - "global GTM should not be loaded on private booking link" , applied on current main

Screenshot 2025-05-24 at 8 42 40 PM

});

test("global GTM should be loaded on non-booking pages", async ({ page, users }) => {
test.skip(!process.env.NEXT_PUBLIC_BODY_SCRIPTS, "Skipping test as NEXT_PUBLIC_BODY_SCRIPTS is not set");
Copy link
Contributor Author

@vijayraghav-io vijayraghav-io May 24, 2025

Choose a reason for hiding this comment

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

This test can be best tested if envNEXT_PUBLIC_BODY_SCRIPTS= is assigned with a value as described in the PR description.
By permanently assigning a value for testing purpose inside .env.example, for the above variable, has some implications -

  • All Non-Booking routes will have gtm.js loaded , all these related test executions will be impacted as an additional external API call will be made always. This may result in increasing the test execution time.

Locally this test alone can be tested with .only and appending the value for the above env variable, for verification without skipping test.

Copy link
Member

@hariombalhara hariombalhara left a comment

Choose a reason for hiding this comment

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

Great fix @vijayraghav-io 🙏

@vijayraghav-io
Copy link
Contributor Author

Thank you! @hariombalhara 🙏

Copy link
Contributor

github-actions bot commented May 25, 2025

E2E results are ready!

@vijayraghav-io
Copy link
Contributor Author

@hariombalhara , all tests are passing 🙏

@hariombalhara hariombalhara merged commit b8ffcef into calcom:main May 26, 2025
33 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
booking-page area: booking page, public booking page, booker 🐛 bug Something isn't working community Created by Linear-GitHub Sync ready-for-e2e
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GTM container meant for dashboard loads on Private Booking links
2 participants