@funnelsgrove/analytics 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,9 @@
1
1
  type GoogleTagEventName = 'purchase';
2
+ export type GoogleTagHeadScript = {
3
+ id: string;
4
+ src?: string;
5
+ content?: string;
6
+ };
2
7
  export type GoogleTagTrackInput = {
3
8
  eventName: GoogleTagEventName;
4
9
  parameters?: Record<string, unknown>;
@@ -10,11 +15,13 @@ export type AnalyticsEventForGoogleTag = {
10
15
  };
11
16
  declare global {
12
17
  interface Window {
18
+ __funnelsgroveGoogleTagHeadReady?: boolean;
13
19
  __funnelsgroveGoogleTagServiceReady?: boolean;
14
20
  dataLayer?: unknown[];
15
21
  gtag?: (...args: unknown[]) => void;
16
22
  }
17
23
  }
24
+ export declare const buildGoogleTagHeadScripts: () => GoogleTagHeadScript[];
18
25
  export declare const mapAnalyticsEventToGoogleTagEvent: (event: AnalyticsEventForGoogleTag) => GoogleTagTrackInput | null;
19
26
  export declare class GoogleTagService {
20
27
  private initialized;
@@ -3,6 +3,51 @@ import { buildBrowserPurchaseAnalyticsAttributes } from './purchase-attributes.s
3
3
  const normalizeEventTarget = (value) => {
4
4
  return value === 'dataLayer' || value === 'gtag' || value === 'both' ? value : 'both';
5
5
  };
6
+ const buildGoogleAnalyticsScriptSrc = (googleAnalyticsId) => `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(googleAnalyticsId)}`;
7
+ const buildGoogleTagManagerScriptSrc = (googleTagManagerId) => `https://www.googletagmanager.com/gtm.js?id=${encodeURIComponent(googleTagManagerId)}`;
8
+ export const buildGoogleTagHeadScripts = () => {
9
+ if (!GOOGLE_TAG_ENABLED || (!GOOGLE_ANALYTICS_ID && !GOOGLE_TAG_MANAGER_ID)) {
10
+ return [];
11
+ }
12
+ const bootstrapLines = [
13
+ 'window.dataLayer = window.dataLayer || [];',
14
+ 'window.gtag = window.gtag || function(){ window.dataLayer.push(arguments); };',
15
+ 'window.__funnelsgroveGoogleTagHeadReady = true;',
16
+ ...(GOOGLE_ANALYTICS_ID
17
+ ? [
18
+ 'window.gtag("js", new Date());',
19
+ `window.gtag("config", ${JSON.stringify(GOOGLE_ANALYTICS_ID)});`,
20
+ ]
21
+ : []),
22
+ ...(GOOGLE_TAG_MANAGER_ID
23
+ ? [
24
+ 'window.dataLayer.push({"gtm.start": Date.now(), event: "gtm.js"});',
25
+ ]
26
+ : []),
27
+ ];
28
+ return [
29
+ {
30
+ id: 'funnelsgrove-google-tag-bootstrap',
31
+ content: bootstrapLines.join('\n'),
32
+ },
33
+ ...(GOOGLE_ANALYTICS_ID
34
+ ? [
35
+ {
36
+ id: 'funnelsgrove-google-analytics',
37
+ src: buildGoogleAnalyticsScriptSrc(GOOGLE_ANALYTICS_ID),
38
+ },
39
+ ]
40
+ : []),
41
+ ...(GOOGLE_TAG_MANAGER_ID
42
+ ? [
43
+ {
44
+ id: 'funnelsgrove-google-tag-manager',
45
+ src: buildGoogleTagManagerScriptSrc(GOOGLE_TAG_MANAGER_ID),
46
+ },
47
+ ]
48
+ : []),
49
+ ];
50
+ };
6
51
  export const mapAnalyticsEventToGoogleTagEvent = (event) => {
7
52
  var _a;
8
53
  switch (event.eventType) {
@@ -53,8 +98,11 @@ export class GoogleTagService {
53
98
  this.initialized = true;
54
99
  window.__funnelsgroveGoogleTagServiceReady = true;
55
100
  this.ensureDataLayer();
101
+ if (window.__funnelsgroveGoogleTagHeadReady) {
102
+ return;
103
+ }
56
104
  if (GOOGLE_ANALYTICS_ID) {
57
- this.installScript(`https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(GOOGLE_ANALYTICS_ID)}`);
105
+ this.installScript(buildGoogleAnalyticsScriptSrc(GOOGLE_ANALYTICS_ID));
58
106
  (_a = window.gtag) === null || _a === void 0 ? void 0 : _a.call(window, 'js', new Date());
59
107
  (_b = window.gtag) === null || _b === void 0 ? void 0 : _b.call(window, 'config', GOOGLE_ANALYTICS_ID);
60
108
  }
@@ -63,7 +111,7 @@ export class GoogleTagService {
63
111
  'gtm.start': Date.now(),
64
112
  event: 'gtm.js',
65
113
  });
66
- this.installScript(`https://www.googletagmanager.com/gtm.js?id=${encodeURIComponent(GOOGLE_TAG_MANAGER_ID)}`);
114
+ this.installScript(buildGoogleTagManagerScriptSrc(GOOGLE_TAG_MANAGER_ID));
67
115
  }
68
116
  }
69
117
  ensureDataLayer() {
@@ -74,9 +122,13 @@ export class GoogleTagService {
74
122
  });
75
123
  }
76
124
  installScript(src) {
125
+ var _a;
77
126
  if (typeof document === 'undefined') {
78
127
  return;
79
128
  }
129
+ if ((_a = document.querySelector) === null || _a === void 0 ? void 0 : _a.call(document, `script[src="${src}"]`)) {
130
+ return;
131
+ }
80
132
  const firstScript = document.getElementsByTagName('script')[0];
81
133
  const parent = firstScript === null || firstScript === void 0 ? void 0 : firstScript.parentNode;
82
134
  if (!parent) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",