Skip to content

Commit

Permalink
feat: add data attributes to script
Browse files Browse the repository at this point in the history
  • Loading branch information
dine committed Jul 29, 2024
1 parent 1ec51bc commit 21a4129
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
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';

/**
* 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 @@ export interface OnReadyOptions {
script: HTMLScriptElement;
}

export interface DataAttributes {
name: string;
value: string;
}

/**
* Options for `loadScript` function.
*/
Expand All @@ -42,6 +47,10 @@ export interface LoadScriptOptions {
* @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 `;`
/**
* Where to append the script element.
*
Expand Down Expand Up @@ -140,6 +149,12 @@ export function loadScript(
script.setAttribute('nonce', config.nonce);
}

if (config.dataAttributes) {
config.dataAttributes.forEach(({ name, value }) => {
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
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,
scriptType: '',
});
expect(script).toBe(document.scripts.item(0));
},
);

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

0 comments on commit 21a4129

Please sign in to comment.