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

refactor: simplify GTM logic + unmount on permission removal #1047

Merged
merged 17 commits into from
Nov 22, 2022
Merged

Conversation

iamacook
Copy link
Member

@iamacook iamacook commented Nov 3, 2022

What it solves

Refactors GTM and adds disale functionality.

How this PR fixes it

  • TagManager.initialize and TagManager.dataLayer have been simplified.
  • GA cookies are removed and the session is reloaded on TagManager,disable
  • Logging now reflects what is being sent to GTM.
  • An unnecessary isGtmLoaded flag was also removed.

How to test it

Refactor

  • GTM should initialise and events should be sent as before. (make sure the intial pageview works especially)
  • No errors/warnings should be shown in the console if the cookie preferences are changed, A reload should occur when revoking permission.
  • When disabling "Analytics" preferences, the GTM tag should be removed. (no tracking visible in Network tab)

Analytics changes

The GTM scripts are all removed on revocation.

@iamacook iamacook self-assigned this Nov 3, 2022
@iamacook iamacook requested a review from usame-algan November 3, 2022 11:43
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Nov 3, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 284f791
Status: ✅  Deploy successful!
Preview URL: https://93729feb.web-core.pages.dev
Branch Preview URL: https://refactor-gtm.web-core.pages.dev

View logs

@github-actions
Copy link

github-actions bot commented Nov 3, 2022

ESLint Summary View Full Report

Annotations are provided inline on the Files Changed tab. You can also see all annotations that were generated on the annotations page.

Type Occurrences Fixable
Errors 0 0
Warnings 0 0
Ignored 0 N/A
  • Result: ✅ success
  • Annotations: 0 total

Report generated by eslint-plus-action

@iamacook iamacook changed the title refactor: GTM refactor: simplify GTM logic + unmount on permission removal Nov 3, 2022
Copy link
Member

@katspaugh katspaugh left a comment

Choose a reason for hiding this comment

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

Left suggestions in the code to allow stacking pre-init events as you had in another PR.

src/services/analytics/TagManager.ts Outdated Show resolved Hide resolved
src/services/analytics/TagManager.ts Outdated Show resolved Hide resolved
@iamacook
Copy link
Member Author

iamacook commented Nov 4, 2022

Left suggestions in the code to allow stacking pre-init events as you had in another PR.

We want to discuss event stacking with legal as the team is concerned regarding consent.

Copy link
Member

@katspaugh katspaugh left a comment

Choose a reason for hiding this comment

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

Looks good!
Let's merge after 1.0.4, tho.

@iamacook iamacook changed the title refactor: simplify GTM logic + unmount on permission removal [Merge AFTER 1.0.4] refactor: simplify GTM logic + unmount on permission removal Nov 4, 2022
@iamacook
Copy link
Member Author

iamacook commented Nov 4, 2022

We want to discuss event stacking with legal as the team is concerned regarding consent.

Just for future reference: @johannesmoormann confirmed that we don't want to stack event prior to cookie agreement.

@github-actions
Copy link

github-actions bot commented Nov 7, 2022

ESLint Summary View Full Report

Annotations are provided inline on the Files Changed tab. You can also see all annotations that were generated on the annotations page.

Type Occurrences Fixable
Errors 0 0
Warnings 0 0
Ignored 0 N/A
  • Result: ✅ success
  • Annotations: 0 total

Report generated by eslint-plus-action

@iamacook iamacook changed the title [Merge AFTER 1.0.4] refactor: simplify GTM logic + unmount on permission removal refactor: simplify GTM logic + unmount on permission removal Nov 9, 2022
@francovenica
Copy link
Contributor

If I enable the "Analytic" checkbox, track a few actions and then I disable the checkbox this error start to show in the console for every action.
image

The error goes away once I reload the page manually

@iamacook
Copy link
Member Author

If I enable the "Analytic" checkbox, track a few actions and then I disable the checkbox this error start to show in the console for every action.

Turns out GTM embeds several scripts of similar names:

I've updated the code and it now removes them all. No error should be shown in the console anymore.

@francovenica
Copy link
Contributor

Tried this again and I can still see the same issue. Can u check if the PR updated correctly?

@iamacook
Copy link
Member Author

Tried this again and I can still see the same issue. Can u check if the PR updated correctly?

Despite unmounting the scripts, they remain in memory. @usame-algan and I will investigate this further.

@iamacook iamacook marked this pull request as draft November 10, 2022 14:18
src/config/constants.ts Outdated Show resolved Hide resolved
@github-actions
Copy link

github-actions bot commented Nov 16, 2022

ESLint Summary View Full Report

Annotations are provided inline on the Files Changed tab. You can also see all annotations that were generated on the annotations page.

Type Occurrences Fixable
Errors 0 0
Warnings 0 0
Ignored 0 N/A
  • Result: ✅ success
  • Annotations: 0 total

Report generated by eslint-plus-action

@iamacook iamacook marked this pull request as ready for review November 20, 2022 15:51
Comment on lines +41 to +44
isInitialized: () => {
const GTM_SCRIPT = 'https://www.googletagmanager.com/gtm.js'

export const _getGtmDataLayerScript = (dataLayer: DataLayer) => {
const script = document.createElement('script')
return !!document.querySelector(`[src^="${GTM_SCRIPT}"]`)
Copy link
Member

Choose a reason for hiding this comment

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

Would it work if we checked that window.dataLayer exists instead? It would be a bit simpler than querying the DOM.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would sooner check the DOM. We could manually push to window.dataLayer and then this would return true when GTM isn't initialised.

Copy link
Member

@usame-algan usame-algan left a comment

Choose a reason for hiding this comment

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

Great idea to reload the page! 💯

@francovenica
Copy link
Contributor

I agree with the solution. I don't think people are enabling/disabling the analytics every 5 mins for the reload to be an inconvenience.
LGTM

@github-actions
Copy link

ESLint Summary View Full Report

Annotations are provided inline on the Files Changed tab. You can also see all annotations that were generated on the annotations page.

Type Occurrences Fixable
Errors 0 0
Warnings 0 0
Ignored 0 N/A
  • Result: ✅ success
  • Annotations: 0 total

Report generated by eslint-plus-action

@github-actions
Copy link

@iamacook iamacook merged commit 0e36eb2 into dev Nov 22, 2022
@iamacook iamacook deleted the refactor-gtm branch November 22, 2022 13:37
@katspaugh katspaugh mentioned this pull request Nov 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants