-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathutils.ts
53 lines (45 loc) · 1.36 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import "url-search-params-polyfill";
import { VueGtmUseOptions } from "./config";
/**
* Load GTM script tag.
*
* @param id GTM ID.
* @param config The config object.
*/
export function loadScript(
id: string,
config: Pick<VueGtmUseOptions, "defer" | "compatibility" | "nonce" | "queryParams"> = {}
): void {
const doc: Document = document;
const script: HTMLScriptElement = doc.createElement("script");
window.dataLayer = window.dataLayer ?? [];
window.dataLayer?.push({
event: "gtm.js",
"gtm.start": new Date().getTime(),
});
if (!id) {
return;
}
script.async = !config.defer;
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
script.defer = Boolean(config.defer || config.compatibility);
if (config.nonce) {
script.nonce = config.nonce;
}
const queryString: URLSearchParams = new URLSearchParams({
id,
...(config.queryParams ?? {}),
});
script.src = `https://www.googletagmanager.com/gtm.js?${queryString}`;
doc.body.appendChild(script);
}
/**
* Check if GTM script is in the document.
*
* @returns `true` if in the `document` is a `script` with `src` containing `googletagmanager.com/gtm.js`, otherwise `false`.
*/
export function hasScript(): boolean {
return Array.from(document.getElementsByTagName("script")).some((script) =>
script.src.includes("googletagmanager.com/gtm.js")
);
}