-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
feat: add data attributes to script #466
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,11 @@ | |||||||||||||
script: HTMLScriptElement; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
export interface DataAttributes { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion:
Suggested change
|
||||||||||||||
name: string; | ||||||||||||||
value: string; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Options for `loadScript` function. | ||||||||||||||
*/ | ||||||||||||||
|
@@ -42,6 +47,10 @@ | |||||||||||||
* @see [Using Google Tag Manager with a Content Security Policy](https://developers.google.com/tag-manager/web/csp) | ||||||||||||||
*/ | ||||||||||||||
nonce?: string; | ||||||||||||||
/** | ||||||||||||||
* Will add data attributes to script tag. | ||||||||||||||
*/ | ||||||||||||||
dataAttributes?: DataAttributes[] | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion:
Suggested change
|
||||||||||||||
/** | ||||||||||||||
* Where to append the script element. | ||||||||||||||
* | ||||||||||||||
|
@@ -140,6 +149,12 @@ | |||||||||||||
script.setAttribute('nonce', config.nonce); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if (config.dataAttributes) { | ||||||||||||||
config.dataAttributes.forEach(({ name, value }) => { | ||||||||||||||
script.setAttribute(`data-${name}`, value); | ||||||||||||||
}); | ||||||||||||||
Comment on lines
+153
to
+155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion:
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if (config.scriptType) { | ||||||||||||||
script.type = config.scriptType; | ||||||||||||||
} | ||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||
import { afterEach, describe, expect, test } from 'vitest'; | ||||
import { hasScript, loadScript } from '../src/index'; | ||||
import type { DynamicDataLayerWindow } from '../src/utils'; | ||||
import type { DataAttributes, DynamicDataLayerWindow } from '../src/utils'; | ||||
import { resetDataLayer, resetHtml } from './test-utils'; | ||||
|
||||
describe('utils', () => { | ||||
|
@@ -24,6 +24,7 @@ describe('utils', () => { | |||
async: boolean; | ||||
defer: boolean; | ||||
nonce: string; | ||||
dataAttributes?: DataAttributes[]; | ||||
scriptType: string; | ||||
}; | ||||
function expectScriptToBeCorrect({ | ||||
|
@@ -160,6 +161,40 @@ describe('utils', () => { | |||
}, | ||||
); | ||||
|
||||
// Test dataAttributes | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: this comment is not needed
Suggested change
|
||||
const dataAttributes: DataAttributes[] = [ | ||||
{ name: 'test', value: 'test' }, | ||||
{ name: 'test2', value: 'test2' }, | ||||
]; | ||||
test( | ||||
JSON.stringify({ | ||||
compatibility: false, | ||||
defer: false, | ||||
dataAttributes, | ||||
}), | ||||
() => { | ||||
expect(window.dataLayer).toBeUndefined(); | ||||
expect(document.scripts.length).toBe(0); | ||||
|
||||
const script: HTMLScriptElement = loadScript('GTM-DEMO', { | ||||
compatibility: false, | ||||
defer: false, | ||||
dataAttributes, | ||||
}); | ||||
|
||||
expectDataLayerToBeCorrect(); | ||||
expectScriptToBeCorrect({ | ||||
src: 'https://www.googletagmanager.com/gtm.js?id=GTM-DEMO', | ||||
async: true, | ||||
defer: false, | ||||
nonce: '', | ||||
dataAttributes, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (blocking): you are passing, but it is not used right now at all |
||||
scriptType: '', | ||||
}); | ||||
expect(script).toBe(document.scripts.item(0)); | ||||
}, | ||||
); | ||||
|
||||
// Test different dataLayer name | ||||
test( | ||||
JSON.stringify({ | ||||
|
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.
suggestion: