Skip to content

Checkout completed and account created GTM events - #3795

Open
dsubak wants to merge 4 commits into
mainfrom
dansubak/202608_add_remaining_gtm_events
Open

Checkout completed and account created GTM events#3795
dsubak wants to merge 4 commits into
mainfrom
dansubak/202608_add_remaining_gtm_events

Conversation

@dsubak

@dsubak dsubak commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Description (What does it do?)

Adds an account created and checkout completed GTM event. Both events have analogs tracked serverside (in a middleware and in hubspot mitxonline tracking code, respectively) but since we can't easily fire GTM events outside of the browser, we needed another approach. I did a bit of poking around Learn+the codebase and a bit of prompting to try and see if there were any more obvious and better approaches, but ultimately figured I'd be better off just sending a PR to folks to validate my thinking.

For account creation, this attempts to hook into onboarding since in testing after registering a new user I got kicked over to the onboarding flow. We use a bit in the session to track whether or not you've already been tracked since it's a multistep process.

For checkout completed, we hook into the enrollment redirect. Here we check a few things about the request and receipt (namely that they're paid and the order isn't pending) and then track the event - there's a useRef to attempt to keep it from firing multiple times, but I'm not entirely sure if it's necessary or if that's a reasonable thing to use.

How can this be tested?

It's a pain! You'll need access to the google tag manager account and to have the GTM values from prod in frontend.local.env for a full-fat test. See gtm.md for the values you need.

If you're not already set up, I recommend the alternative tests laid out. They're not perfect, but they are good enough to validate that it doesn't break anything and that it does fire the event.

Account Created

For either version of the test you'll want to use a fresh incognito browser since we store a bit in the session to keep the event from firing more than once during an onboarding flow.

Full Test

Screenshot 2026-08-25 at 4 59 46 PM

Test w/o GTM setup or account:

  • Visit your local, open the console and enter window.dataLayer - it should NOT show an account-created event.
  • Hit the Log In button and go through the Keycloak registration redirection and flow. You should be returned to the Onboarding wizard once you successfully complete the registration process.
  • Open your console again and examine window.dataLayer. It should now have an additional entry for the account-created event. This is as good an analog as you'll be able to get without a full setup.
Screenshot 2026-08-25 at 4 48 39 PM

Checkout Completed

Test w/o GTM setup or account

This is slightly scuffed, but should be representative.

  • Ensure you've got a paid order for a user you're logged in as.
  • Visit your local, open the console and enter window.dataLayer - it should NOT show an checkout-completed event.
  • Next, visit http://learn.odl.local:9080/dashboard?order_status=fulfilled&order_id=<order_pk>
  • Open the console and enter window.dataLayer, it should now show an checkout-completed with values corresponding to the order you specified.
Screenshot 2026-08-26 at 10 04 34 AM

In theory, it should be testable if you have a full e2e checkout workflow set up between learn and mitxonline but I've yet to verify it in tag assistant.

@github-actions

github-actions Bot commented Aug 19, 2026

Copy link
Copy Markdown

OpenAPI Changes

No changes detected

View full changelog

Unexpected changes? Ensure your branch is up-to-date with main (consider rebasing).

@cp-at-mit cp-at-mit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems good to me

@dsubak
dsubak force-pushed the dansubak/202608_add_remaining_gtm_events branch from 7005164 to f7f5526 Compare August 25, 2026 20:13
@dsubak dsubak changed the title WIP checkout completed and account created events Checkout completed and account created events Aug 26, 2026
@dsubak
dsubak marked this pull request as ready for review August 26, 2026 14:19
@dsubak
dsubak requested a review from a team as a code owner August 26, 2026 14:19
Copilot AI balanced review requested due to automatic review settings August 26, 2026 14:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds GTM tracking for account creation and completed paid checkouts.

Changes:

  • Adds and tests two GTM event helpers.
  • Triggers account-created tracking during onboarding.
  • Triggers checkout-completed tracking after enrollment redirects.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
frontends/main/src/common/analytics/gtm.ts Defines the new GTM events.
frontends/main/src/common/analytics/gtm.test.ts Tests event payload generation.
frontends/main/src/app-pages/OnboardingPage/OnboardingPage.tsx Emits account-created during onboarding.
frontends/main/src/app-pages/OnboardingPage/OnboardingPage.test.tsx Tests onboarding event deduplication.
frontends/main/src/app-pages/DashboardPage/EnrollmentRedirectAlert.tsx Emits checkout-completed from receipt data.
frontends/main/src/app-pages/DashboardPage/EnrollmentRedirectAlert.test.tsx Tests checkout event behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread frontends/main/src/app-pages/OnboardingPage/OnboardingPage.tsx Outdated
@dsubak dsubak changed the title Checkout completed and account created events Checkout completed and account created GTM events Aug 26, 2026
if (!profile) return

let alreadyTracked = false
try {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this effect will fire trackAccountCreated() whenever /onboarding renders with a loaded profile and the sessionStorage key isn't already set but it has no "this account was just created" signal so it fire for existing users.

Screen.Recording.2026-08-27.at.2.37.06.PM.mov

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good callout - I was operating on the assumption that the main way folks are gonna encounter /onboarding is via redirection after account creation and that afterwards you're pretty unlikely to go to that page, but I'll double check that.

If it turns out we actually get a lot of return visitors to that path this is gonna need something a bit more involved to get right

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You could have he backend pass an explicit signal through the redirect e.g. append ?new=1 alongside next in CustomLoginView.get (mirroring the existing skip_onboarding=1 pattern) and gate trackAccountCreated() on that param instead of relying on sessionStorage alone. Keep the session-storage check only to prevent re-firing within a single legitimate new-account flow, not as the sole gate for "is this a new account" at all.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was operating on the assumption that the main way folks are gonna encounter /onboarding is via redirection after account creation

You are probably correct and this edge case is not likely which is why I approved the PR to come up. I just wanted to call it to you attention.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, I didn't realize we already have a has_logged_in bit on the backend that appears correct - I'll get a query param piped in based on that and that ought to address the issue.

@daniellefrappier18 daniellefrappier18 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was able to verify the testing path w/o GTM setup

But something you should probably look at before merging is account-created fires for existing users, not just new account. See my comment above

@dsubak
dsubak force-pushed the dansubak/202608_add_remaining_gtm_events branch from c9c5762 to 90a7501 Compare August 28, 2026 19:18
@dsubak
dsubak force-pushed the dansubak/202608_add_remaining_gtm_events branch from 9e6e1fb to a9d4b05 Compare August 28, 2026 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants