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

feat: add data attributes to script #466

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ export { GtmSupport } from './gtm-support';
export type { TrackEventOptions } from './gtm-support';
export type { GtmSupportOptions } from './options';
export { hasScript, loadScript } from './utils';
export type { LoadScriptOptions, OnReadyOptions } from './utils';
export type {
DataAttributes,
LoadScriptOptions,
OnReadyOptions,
} from './utils';
5 changes: 5 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { GtmIdContainer, GtmQueryParams } from './gtm-container';
import { type DataAttributes } from './utils';
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion:

Suggested change
import { type DataAttributes } from './utils';
import type { DataAttributes } from './utils';


/**
* Options passed to GTM Support.
Expand Down Expand Up @@ -58,6 +59,10 @@ export interface GtmSupportOptions {
* @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[];
/**
* Where to append the script element.
*
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
script: HTMLScriptElement;
}

export interface DataAttributes {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion:
interfaces should use singular

Suggested change
export interface DataAttributes {
export interface DataAttribute {

name: string;
value: string;
}

/**
* Options for `loadScript` function.
*/
Expand All @@ -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[]

Check failure on line 53 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Lint: node-22, ubuntu-latest

Insert `;`
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion:

Suggested change
dataAttributes?: DataAttributes[]
dataAttributes?: DataAttributes[];

/**
* Where to append the script element.
*
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion:

Suggested change
config.dataAttributes.forEach(({ name, value }) => {
script.setAttribute(`data-${name}`, value);
});
for ({ name, value } of config.dataAttributes) {
script.setAttribute(`data-${name}`, value);
}

}

if (config.scriptType) {
script.type = config.scriptType;
}
Expand Down
37 changes: 36 additions & 1 deletion tests/utils.test.ts
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', () => {
Expand All @@ -24,6 +24,7 @@ describe('utils', () => {
async: boolean;
defer: boolean;
nonce: string;
dataAttributes?: DataAttributes[];
scriptType: string;
};
function expectScriptToBeCorrect({
Expand Down Expand Up @@ -160,6 +161,40 @@ describe('utils', () => {
},
);

// Test dataAttributes
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: this comment is not needed

Suggested change
// Test dataAttributes

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,
Copy link
Contributor

Choose a reason for hiding this comment

The 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
please change the expectScriptToBeCorrect to test internally for data attribute correctness

scriptType: '',
});
expect(script).toBe(document.scripts.item(0));
},
);

// Test different dataLayer name
test(
JSON.stringify({
Expand Down
Loading