ninetwo-user-tracking 1.0.9 → 1.0.11

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
@@ -36,14 +36,28 @@ var import_react = require("react");
36
36
  var pushToDataLayer = (props) => {
37
37
  if (typeof window === "undefined")
38
38
  return;
39
- const dataLayer = window.dataLayer || [];
40
- dataLayer.push({
41
- event: props.event,
42
- // Nome do evento (ex: 'click_cta')
43
- event_category: props.category,
44
- event_label: props.label,
45
- event_type: props.type,
46
- interaction_time: (/* @__PURE__ */ new Date()).toISOString()
39
+ const { event, category, label, type, ...rest } = props;
40
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
41
+ const params = {
42
+ event_category: category,
43
+ event_label: label,
44
+ event_type: type,
45
+ interaction_time: timestamp,
46
+ ...rest
47
+ // permite enviar dados extras
48
+ };
49
+ if (typeof window.gtag === "function") {
50
+ try {
51
+ window.gtag("event", event, params);
52
+ return;
53
+ } catch (err) {
54
+ console.warn("[NineTwo Tracking] gtag falhou, fallback para dataLayer", err);
55
+ }
56
+ }
57
+ window.dataLayer = window.dataLayer || [];
58
+ window.dataLayer.push({
59
+ event,
60
+ ...params
47
61
  });
48
62
  };
49
63
 
@@ -120,34 +134,78 @@ var TrackingProvider = ({
120
134
  useAutoTrackSubmit(true);
121
135
  (0, import_react3.useEffect)(() => {
122
136
  if (!gtmId || typeof window === "undefined") {
123
- if (debug && !gtmId)
124
- console.warn("[NineTwo Tracking] GTM ID n\xE3o fornecido.");
137
+ if (debug && !gtmId) {
138
+ console.warn("[NineTwo Tracking] Nenhum ID fornecido.");
139
+ }
125
140
  return;
126
141
  }
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.");
142
+ const isGA4 = gtmId.startsWith("G-");
143
+ const isGTM = gtmId.startsWith("GTM-");
144
+ if (!isGA4 && !isGTM) {
145
+ console.warn("[NineTwo Tracking] ID inv\xE1lido:", gtmId);
146
+ return;
147
+ }
148
+ const existing = document.querySelector(
149
+ `script[data-ninetwo-tracking="${gtmId}"]`
150
+ );
151
+ if (existing) {
152
+ if (debug) {
153
+ console.log("[NineTwo Tracking] Script j\xE1 carregado:", gtmId);
154
+ }
131
155
  return;
132
156
  }
133
157
  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);
158
+ if (isGA4) {
159
+ if (debug) {
160
+ console.log("[NineTwo Tracking] Inicializando GA4:", gtmId);
161
+ }
162
+ const gtagScript = document.createElement("script");
163
+ gtagScript.async = true;
164
+ gtagScript.src = `https://www.googletagmanager.com/gtag/js?id=${gtmId}`;
165
+ gtagScript.setAttribute("data-ninetwo-tracking", gtmId);
166
+ gtagScript.onload = () => {
167
+ if (debug) {
168
+ console.log("[NineTwo Tracking] gtag.js carregado com sucesso");
169
+ }
170
+ };
171
+ gtagScript.onerror = () => {
172
+ console.error("[NineTwo Tracking] Erro ao carregar gtag.js");
173
+ };
174
+ document.head.appendChild(gtagScript);
175
+ const inlineScript = document.createElement("script");
176
+ inlineScript.setAttribute("data-ninetwo-tracking", `${gtmId}-inline`);
177
+ inlineScript.innerHTML = `
178
+ window.dataLayer = window.dataLayer || [];
179
+ function gtag(){dataLayer.push(arguments);}
180
+ gtag('js', new Date());
181
+ gtag('config', '${gtmId}', {
182
+ send_page_view: true
183
+ });
184
+ `;
185
+ document.head.appendChild(inlineScript);
186
+ }
187
+ if (isGTM) {
188
+ if (debug) {
189
+ console.log("[NineTwo Tracking] Inicializando GTM:", gtmId);
190
+ }
191
+ window.dataLayer.push({
192
+ "gtm.start": (/* @__PURE__ */ new Date()).getTime(),
193
+ event: "gtm.js"
194
+ });
195
+ const gtmScript = document.createElement("script");
196
+ gtmScript.async = true;
197
+ gtmScript.src = `https://www.googletagmanager.com/gtm.js?id=${gtmId}`;
198
+ gtmScript.setAttribute("data-ninetwo-tracking", gtmId);
199
+ gtmScript.onload = () => {
200
+ if (debug) {
201
+ console.log("[NineTwo Tracking] GTM carregado com sucesso");
202
+ }
203
+ };
204
+ gtmScript.onerror = () => {
205
+ console.error("[NineTwo Tracking] Erro ao carregar GTM");
206
+ };
207
+ document.head.appendChild(gtmScript);
208
+ }
151
209
  }, [gtmId, debug]);
152
210
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
153
211
  };
package/dist/index.mjs CHANGED
@@ -8,14 +8,28 @@ import { useEffect } from "react";
8
8
  var pushToDataLayer = (props) => {
9
9
  if (typeof window === "undefined")
10
10
  return;
11
- const dataLayer = window.dataLayer || [];
12
- dataLayer.push({
13
- event: props.event,
14
- // Nome do evento (ex: 'click_cta')
15
- event_category: props.category,
16
- event_label: props.label,
17
- event_type: props.type,
18
- interaction_time: (/* @__PURE__ */ new Date()).toISOString()
11
+ const { event, category, label, type, ...rest } = props;
12
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
13
+ const params = {
14
+ event_category: category,
15
+ event_label: label,
16
+ event_type: type,
17
+ interaction_time: timestamp,
18
+ ...rest
19
+ // permite enviar dados extras
20
+ };
21
+ if (typeof window.gtag === "function") {
22
+ try {
23
+ window.gtag("event", event, params);
24
+ return;
25
+ } catch (err) {
26
+ console.warn("[NineTwo Tracking] gtag falhou, fallback para dataLayer", err);
27
+ }
28
+ }
29
+ window.dataLayer = window.dataLayer || [];
30
+ window.dataLayer.push({
31
+ event,
32
+ ...params
19
33
  });
20
34
  };
21
35
 
@@ -92,34 +106,78 @@ var TrackingProvider = ({
92
106
  useAutoTrackSubmit(true);
93
107
  useEffect3(() => {
94
108
  if (!gtmId || typeof window === "undefined") {
95
- if (debug && !gtmId)
96
- console.warn("[NineTwo Tracking] GTM ID n\xE3o fornecido.");
109
+ if (debug && !gtmId) {
110
+ console.warn("[NineTwo Tracking] Nenhum ID fornecido.");
111
+ }
112
+ return;
113
+ }
114
+ const isGA4 = gtmId.startsWith("G-");
115
+ const isGTM = gtmId.startsWith("GTM-");
116
+ if (!isGA4 && !isGTM) {
117
+ console.warn("[NineTwo Tracking] ID inv\xE1lido:", gtmId);
97
118
  return;
98
119
  }
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.");
120
+ const existing = document.querySelector(
121
+ `script[data-ninetwo-tracking="${gtmId}"]`
122
+ );
123
+ if (existing) {
124
+ if (debug) {
125
+ console.log("[NineTwo Tracking] Script j\xE1 carregado:", gtmId);
126
+ }
103
127
  return;
104
128
  }
105
129
  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);
130
+ if (isGA4) {
131
+ if (debug) {
132
+ console.log("[NineTwo Tracking] Inicializando GA4:", gtmId);
133
+ }
134
+ const gtagScript = document.createElement("script");
135
+ gtagScript.async = true;
136
+ gtagScript.src = `https://www.googletagmanager.com/gtag/js?id=${gtmId}`;
137
+ gtagScript.setAttribute("data-ninetwo-tracking", gtmId);
138
+ gtagScript.onload = () => {
139
+ if (debug) {
140
+ console.log("[NineTwo Tracking] gtag.js carregado com sucesso");
141
+ }
142
+ };
143
+ gtagScript.onerror = () => {
144
+ console.error("[NineTwo Tracking] Erro ao carregar gtag.js");
145
+ };
146
+ document.head.appendChild(gtagScript);
147
+ const inlineScript = document.createElement("script");
148
+ inlineScript.setAttribute("data-ninetwo-tracking", `${gtmId}-inline`);
149
+ inlineScript.innerHTML = `
150
+ window.dataLayer = window.dataLayer || [];
151
+ function gtag(){dataLayer.push(arguments);}
152
+ gtag('js', new Date());
153
+ gtag('config', '${gtmId}', {
154
+ send_page_view: true
155
+ });
156
+ `;
157
+ document.head.appendChild(inlineScript);
158
+ }
159
+ if (isGTM) {
160
+ if (debug) {
161
+ console.log("[NineTwo Tracking] Inicializando GTM:", gtmId);
162
+ }
163
+ window.dataLayer.push({
164
+ "gtm.start": (/* @__PURE__ */ new Date()).getTime(),
165
+ event: "gtm.js"
166
+ });
167
+ const gtmScript = document.createElement("script");
168
+ gtmScript.async = true;
169
+ gtmScript.src = `https://www.googletagmanager.com/gtm.js?id=${gtmId}`;
170
+ gtmScript.setAttribute("data-ninetwo-tracking", gtmId);
171
+ gtmScript.onload = () => {
172
+ if (debug) {
173
+ console.log("[NineTwo Tracking] GTM carregado com sucesso");
174
+ }
175
+ };
176
+ gtmScript.onerror = () => {
177
+ console.error("[NineTwo Tracking] Erro ao carregar GTM");
178
+ };
179
+ document.head.appendChild(gtmScript);
180
+ }
123
181
  }, [gtmId, debug]);
124
182
  return /* @__PURE__ */ jsx(Fragment, { children });
125
183
  };
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.11",
4
4
  "description": "User tracking abstraction for React/Nextjs with GTM",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",