-
Notifications
You must be signed in to change notification settings - Fork 465
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
+185
−142
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4c4f9bb
feat: add `TagManager.destroy` function
iamacook c0c39fb
refactor: GTM
iamacook 55adb40
fix: add dev log
iamacook d3ca65d
Merge branch 'dev' into refactor-gtm
iamacook a86cfc9
Merge branch 'dev' into refactor-gtm
iamacook 732989e
fix: extract constant
iamacook 266ed6a
Merge branch 'dev' into refactor-gtm
iamacook d0db466
fix: remove all GTM scripts
iamacook 0c84c2c
fix: disable GTM/GA programmatically
iamacook dbefb6d
Merge branch 'dev' into refactor-gtm
iamacook 2bc7cee
Merge branch 'dev' into refactor-gtm
iamacook 63c372e
fix: remove cookies
iamacook 9822f0a
fix: adjust tests
iamacook 178e172
fix: reload instead of using flag
iamacook c0ca8bc
fix: tests
iamacook 284f791
Merge branch 'dev' into refactor-gtm
iamacook c156d97
Merge branch 'dev' into refactor-gtm
iamacook File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,160 @@ | ||
import TagManager, { _getGtmDataLayerScript, _getGtmScript, _getRequiredGtmArgs } from '../TagManager' | ||
import Cookies from 'js-cookie' | ||
|
||
const MOCK_ID = 'GTM-123456' | ||
import * as gtm from '../TagManager' | ||
|
||
describe('TagManager', () => { | ||
beforeEach(() => { | ||
delete window.dataLayer | ||
}) | ||
const { default: TagManager } = gtm | ||
|
||
describe('getRequiredGtmArgs', () => { | ||
it('should assign default arguments', () => { | ||
const result1 = _getRequiredGtmArgs({ gtmId: MOCK_ID }) | ||
const MOCK_ID = 'GTM-123456' | ||
const MOCK_AUTH = 'key123' | ||
const MOCK_PREVIEW = 'env-0' | ||
|
||
expect(result1).toStrictEqual({ | ||
gtmId: MOCK_ID, | ||
dataLayer: undefined, | ||
auth: '', | ||
preview: '', | ||
}) | ||
jest.mock('js-cookie', () => ({ | ||
remove: jest.fn(), | ||
})) | ||
|
||
const result2 = _getRequiredGtmArgs({ gtmId: MOCK_ID, auth: 'abcdefg', preview: 'env-1' }) | ||
describe('TagManager', () => { | ||
const originalLocation = window.location | ||
|
||
// Mock `location.reload` | ||
beforeAll(() => { | ||
Object.defineProperty(window, 'location', { | ||
configurable: true, | ||
value: { | ||
...originalLocation, | ||
reload: jest.fn(), | ||
}, | ||
}) | ||
}) | ||
|
||
expect(result2).toStrictEqual({ | ||
gtmId: MOCK_ID, | ||
dataLayer: undefined, | ||
auth: '>m_auth=abcdefg', | ||
preview: '>m_preview=env-1', | ||
}) | ||
// Remove mock | ||
afterAll(() => { | ||
Object.defineProperty(window, 'location', { | ||
configurable: true, | ||
value: originalLocation, | ||
}) | ||
}) | ||
|
||
describe('getGtmScript', () => { | ||
it('should use the id', () => { | ||
const script1 = _getGtmScript({ gtmId: MOCK_ID }) | ||
// Clear GTM between tests | ||
afterEach(() => { | ||
document.head.innerHTML = '' | ||
delete window.dataLayer | ||
}) | ||
|
||
describe('TagManager._getScript', () => { | ||
it('should use the id, auth and preview', () => { | ||
const script1 = TagManager._getScript({ gtmId: MOCK_ID, auth: MOCK_AUTH, preview: MOCK_PREVIEW }) | ||
|
||
expect(script1.innerHTML).toContain(MOCK_ID) | ||
expect(script1.innerHTML).toContain(`>m_auth=${MOCK_AUTH}`) | ||
expect(script1.innerHTML).toContain(`>m_preview=${MOCK_PREVIEW}`) | ||
expect(script1.innerHTML).toContain('dataLayer') | ||
}) | ||
}) | ||
|
||
it('should use the auth and preview if present', () => { | ||
const script1 = _getGtmScript({ | ||
gtmId: MOCK_ID, | ||
}) | ||
|
||
expect(script1.innerHTML).not.toContain('>m_auth') | ||
expect(script1.innerHTML).not.toContain('>m_preview') | ||
|
||
const script2 = _getGtmScript({ | ||
gtmId: MOCK_ID, | ||
auth: 'abcdefg', | ||
preview: 'env-1', | ||
}) | ||
|
||
expect(script2.innerHTML).toContain('>m_auth=abcdefg>m_preview=env-1') | ||
describe('TagManager.isInitialized', () => { | ||
it('should return false if no script is found', () => { | ||
expect(TagManager.isInitialized()).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('getGtmDataLayerScript', () => { | ||
it('should use the `dataLayer` for the script', () => { | ||
const dataLayerScript = _getGtmDataLayerScript({ | ||
gtmId: MOCK_ID, | ||
dataLayer: { foo: 'bar' }, | ||
}) | ||
it('should return true if a script is found', () => { | ||
TagManager.initialize({ gtmId: MOCK_ID, auth: MOCK_AUTH, preview: MOCK_PREVIEW }) | ||
|
||
expect(dataLayerScript.innerHTML).toContain('{"foo":"bar"}') | ||
expect(TagManager.isInitialized()).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('TagManager.initialize', () => { | ||
it('should initialize TagManager', () => { | ||
TagManager.initialize({ gtmId: MOCK_ID }) | ||
TagManager.initialize({ gtmId: MOCK_ID, auth: MOCK_AUTH, preview: MOCK_PREVIEW }) | ||
|
||
expect(document.head.childNodes).toHaveLength(2) | ||
|
||
// Script added by `TagManager._getScript` | ||
// @ts-expect-error | ||
expect(document.head.childNodes[0].src).toBe( | ||
`https://www.googletagmanager.com/gtm.js?id=${MOCK_ID}>m_auth=${MOCK_AUTH}>m_preview=${MOCK_PREVIEW}>m_cookies_win=x`, | ||
) | ||
|
||
// Manually added script | ||
expect(document.head.childNodes[1]).toStrictEqual( | ||
TagManager._getScript({ gtmId: MOCK_ID, auth: MOCK_AUTH, preview: MOCK_PREVIEW }), | ||
) | ||
|
||
expect(window.dataLayer).toHaveLength(1) | ||
expect(window.dataLayer[0]).toStrictEqual({ event: 'gtm.js', 'gtm.start': expect.any(Number) }) | ||
}) | ||
|
||
it('should not re-initialize the scripts if previously enabled', async () => { | ||
const getScriptSpy = jest.spyOn(gtm.default, '_getScript') | ||
|
||
TagManager.initialize({ gtmId: MOCK_ID, auth: MOCK_AUTH, preview: MOCK_PREVIEW }) | ||
TagManager.initialize({ gtmId: MOCK_ID, auth: MOCK_AUTH, preview: MOCK_PREVIEW }) | ||
|
||
expect(getScriptSpy).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should push to the dataLayer if povided', () => { | ||
TagManager.initialize({ gtmId: MOCK_ID, auth: MOCK_AUTH, preview: MOCK_PREVIEW, dataLayer: { test: '456' } }) | ||
|
||
expect(window.dataLayer).toHaveLength(2) | ||
expect(window.dataLayer[0]).toStrictEqual({ test: '456' }) | ||
expect(window.dataLayer[1]).toStrictEqual({ event: 'gtm.js', 'gtm.start': expect.any(Number) }) | ||
}) | ||
}) | ||
|
||
describe('TagManager.dataLayer', () => { | ||
it('should not push to the dataLayer if not initialized', () => { | ||
TagManager.dataLayer({ test: '456' }) | ||
|
||
expect(window.dataLayer).toBeUndefined() | ||
}) | ||
|
||
it('should push data to the dataLayer', () => { | ||
TagManager.dataLayer({ | ||
test: '123', | ||
expect(window.dataLayer).toBeUndefined() | ||
|
||
TagManager.initialize({ | ||
gtmId: MOCK_ID, | ||
auth: MOCK_AUTH, | ||
preview: MOCK_PREVIEW, | ||
}) | ||
|
||
expect(window.dataLayer).toHaveLength(1) | ||
expect(window.dataLayer[0]).toStrictEqual({ | ||
expect(window.dataLayer[0]).toStrictEqual({ event: 'gtm.js', 'gtm.start': expect.any(Number) }) | ||
|
||
TagManager.dataLayer({ | ||
test: '123', | ||
}) | ||
|
||
expect(window.dataLayer).toHaveLength(2) | ||
expect(window.dataLayer[1]).toStrictEqual({ test: '123' }) | ||
}) | ||
}) | ||
|
||
describe('TagManager.disable', () => { | ||
it('should not remove GA cookies and reload if not mounted', () => { | ||
TagManager.disable() | ||
|
||
expect(Cookies.remove).not.toHaveBeenCalled() | ||
|
||
expect(global.location.reload).not.toHaveBeenCalled() | ||
}) | ||
it('should remove GA cookies and reload if mounted', () => { | ||
TagManager.initialize({ | ||
gtmId: MOCK_ID, | ||
auth: MOCK_AUTH, | ||
preview: MOCK_PREVIEW, | ||
}) | ||
|
||
TagManager.disable() | ||
|
||
const path = '/' | ||
const domain = '.localhost' | ||
|
||
expect(Cookies.remove).toHaveBeenCalledWith('_ga', { path, domain }) | ||
expect(Cookies.remove).toHaveBeenCalledWith('_gat', { path, domain }) | ||
expect(Cookies.remove).toHaveBeenCalledWith('_gid', { path, domain }) | ||
|
||
expect(global.location.reload).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 returntrue
when GTM isn't initialised.