ninetwo-user-tracking 1.0.9 → 1.0.10

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.
package/dist/index.js CHANGED
@@ -120,34 +120,78 @@ var TrackingProvider = ({
120
120
  useAutoTrackSubmit(true);
121
121
  (0, import_react3.useEffect)(() => {
122
122
  if (!gtmId || typeof window === "undefined") {
123
- if (debug && !gtmId)
124
- console.warn("[NineTwo Tracking] GTM ID n\xE3o fornecido.");
123
+ if (debug && !gtmId) {
124
+ console.warn("[NineTwo Tracking] Nenhum ID fornecido.");
125
+ }
125
126
  return;
126
127
  }
127
- const scriptId = "ninetwo-gtm-script";
128
- if (document.getElementById(scriptId)) {
129
- if (debug)
130
- console.log("[NineTwo Tracking] Script j\xE1 existente. Ignorando reinje\xE7\xE3o.");
128
+ const isGA4 = gtmId.startsWith("G-");
129
+ const isGTM = gtmId.startsWith("GTM-");
130
+ if (!isGA4 && !isGTM) {
131
+ console.warn("[NineTwo Tracking] ID inv\xE1lido:", gtmId);
132
+ return;
133
+ }
134
+ const existing = document.querySelector(
135
+ `script[data-ninetwo-tracking="${gtmId}"]`
136
+ );
137
+ if (existing) {
138
+ if (debug) {
139
+ console.log("[NineTwo Tracking] Script j\xE1 carregado:", gtmId);
140
+ }
131
141
  return;
132
142
  }
133
143
  window.dataLayer = window.dataLayer || [];
134
- window.dataLayer.push({
135
- "gtm.start": (/* @__PURE__ */ new Date()).getTime(),
136
- event: "gtm.js"
137
- });
138
- const script = document.createElement("script");
139
- script.id = scriptId;
140
- script.async = true;
141
- script.src = `https://www.googletagmanager.com/gtm.js?id=${gtmId}`;
142
- script.onload = () => {
143
- if (debug)
144
- console.log(`[NineTwo Tracking] \u2705 GTM carregado com sucesso! (${gtmId})`);
145
- };
146
- script.onerror = () => {
147
- if (debug)
148
- console.error("[NineTwo Tracking] \u274C Erro ao carregar GTM.");
149
- };
150
- document.head.appendChild(script);
144
+ if (isGA4) {
145
+ if (debug) {
146
+ console.log("[NineTwo Tracking] Inicializando GA4:", gtmId);
147
+ }
148
+ const gtagScript = document.createElement("script");
149
+ gtagScript.async = true;
150
+ gtagScript.src = `https://www.googletagmanager.com/gtag/js?id=${gtmId}`;
151
+ gtagScript.setAttribute("data-ninetwo-tracking", gtmId);
152
+ gtagScript.onload = () => {
153
+ if (debug) {
154
+ console.log("[NineTwo Tracking] gtag.js carregado com sucesso");
155
+ }
156
+ };
157
+ gtagScript.onerror = () => {
158
+ console.error("[NineTwo Tracking] Erro ao carregar gtag.js");
159
+ };
160
+ document.head.appendChild(gtagScript);
161
+ const inlineScript = document.createElement("script");
162
+ inlineScript.setAttribute("data-ninetwo-tracking", `${gtmId}-inline`);
163
+ inlineScript.innerHTML = `
164
+ window.dataLayer = window.dataLayer || [];
165
+ function gtag(){dataLayer.push(arguments);}
166
+ gtag('js', new Date());
167
+ gtag('config', '${gtmId}', {
168
+ send_page_view: true
169
+ });
170
+ `;
171
+ document.head.appendChild(inlineScript);
172
+ }
173
+ if (isGTM) {
174
+ if (debug) {
175
+ console.log("[NineTwo Tracking] Inicializando GTM:", gtmId);
176
+ }
177
+ window.dataLayer.push({
178
+ "gtm.start": (/* @__PURE__ */ new Date()).getTime(),
179
+ event: "gtm.js"
180
+ });
181
+ const gtmScript = document.createElement("script");
182
+ gtmScript.async = true;
183
+ gtmScript.src = `https://www.googletagmanager.com/gtm.js?id=${gtmId}`;
184
+ gtmScript.setAttribute("data-ninetwo-tracking", gtmId);
185
+ gtmScript.onload = () => {
186
+ if (debug) {
187
+ console.log("[NineTwo Tracking] GTM carregado com sucesso");
188
+ }
189
+ };
190
+ gtmScript.onerror = () => {
191
+ console.error("[NineTwo Tracking] Erro ao carregar GTM");
192
+ };
193
+ document.head.appendChild(gtmScript);
194
+ }
151
195
  }, [gtmId, debug]);
152
196
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
153
197
  };
package/dist/index.mjs CHANGED
@@ -92,34 +92,78 @@ var TrackingProvider = ({
92
92
  useAutoTrackSubmit(true);
93
93
  useEffect3(() => {
94
94
  if (!gtmId || typeof window === "undefined") {
95
- if (debug && !gtmId)
96
- console.warn("[NineTwo Tracking] GTM ID n\xE3o fornecido.");
95
+ if (debug && !gtmId) {
96
+ console.warn("[NineTwo Tracking] Nenhum ID fornecido.");
97
+ }
97
98
  return;
98
99
  }
99
- const scriptId = "ninetwo-gtm-script";
100
- if (document.getElementById(scriptId)) {
101
- if (debug)
102
- console.log("[NineTwo Tracking] Script j\xE1 existente. Ignorando reinje\xE7\xE3o.");
100
+ const isGA4 = gtmId.startsWith("G-");
101
+ const isGTM = gtmId.startsWith("GTM-");
102
+ if (!isGA4 && !isGTM) {
103
+ console.warn("[NineTwo Tracking] ID inv\xE1lido:", gtmId);
104
+ return;
105
+ }
106
+ const existing = document.querySelector(
107
+ `script[data-ninetwo-tracking="${gtmId}"]`
108
+ );
109
+ if (existing) {
110
+ if (debug) {
111
+ console.log("[NineTwo Tracking] Script j\xE1 carregado:", gtmId);
112
+ }
103
113
  return;
104
114
  }
105
115
  window.dataLayer = window.dataLayer || [];
106
- window.dataLayer.push({
107
- "gtm.start": (/* @__PURE__ */ new Date()).getTime(),
108
- event: "gtm.js"
109
- });
110
- const script = document.createElement("script");
111
- script.id = scriptId;
112
- script.async = true;
113
- script.src = `https://www.googletagmanager.com/gtm.js?id=${gtmId}`;
114
- script.onload = () => {
115
- if (debug)
116
- console.log(`[NineTwo Tracking] \u2705 GTM carregado com sucesso! (${gtmId})`);
117
- };
118
- script.onerror = () => {
119
- if (debug)
120
- console.error("[NineTwo Tracking] \u274C Erro ao carregar GTM.");
121
- };
122
- document.head.appendChild(script);
116
+ if (isGA4) {
117
+ if (debug) {
118
+ console.log("[NineTwo Tracking] Inicializando GA4:", gtmId);
119
+ }
120
+ const gtagScript = document.createElement("script");
121
+ gtagScript.async = true;
122
+ gtagScript.src = `https://www.googletagmanager.com/gtag/js?id=${gtmId}`;
123
+ gtagScript.setAttribute("data-ninetwo-tracking", gtmId);
124
+ gtagScript.onload = () => {
125
+ if (debug) {
126
+ console.log("[NineTwo Tracking] gtag.js carregado com sucesso");
127
+ }
128
+ };
129
+ gtagScript.onerror = () => {
130
+ console.error("[NineTwo Tracking] Erro ao carregar gtag.js");
131
+ };
132
+ document.head.appendChild(gtagScript);
133
+ const inlineScript = document.createElement("script");
134
+ inlineScript.setAttribute("data-ninetwo-tracking", `${gtmId}-inline`);
135
+ inlineScript.innerHTML = `
136
+ window.dataLayer = window.dataLayer || [];
137
+ function gtag(){dataLayer.push(arguments);}
138
+ gtag('js', new Date());
139
+ gtag('config', '${gtmId}', {
140
+ send_page_view: true
141
+ });
142
+ `;
143
+ document.head.appendChild(inlineScript);
144
+ }
145
+ if (isGTM) {
146
+ if (debug) {
147
+ console.log("[NineTwo Tracking] Inicializando GTM:", gtmId);
148
+ }
149
+ window.dataLayer.push({
150
+ "gtm.start": (/* @__PURE__ */ new Date()).getTime(),
151
+ event: "gtm.js"
152
+ });
153
+ const gtmScript = document.createElement("script");
154
+ gtmScript.async = true;
155
+ gtmScript.src = `https://www.googletagmanager.com/gtm.js?id=${gtmId}`;
156
+ gtmScript.setAttribute("data-ninetwo-tracking", gtmId);
157
+ gtmScript.onload = () => {
158
+ if (debug) {
159
+ console.log("[NineTwo Tracking] GTM carregado com sucesso");
160
+ }
161
+ };
162
+ gtmScript.onerror = () => {
163
+ console.error("[NineTwo Tracking] Erro ao carregar GTM");
164
+ };
165
+ document.head.appendChild(gtmScript);
166
+ }
123
167
  }, [gtmId, debug]);
124
168
  return /* @__PURE__ */ jsx(Fragment, { children });
125
169
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ninetwo-user-tracking",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "User tracking abstraction for React/Nextjs with GTM",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",