@arkyn/components 1.4.19 → 1.4.21

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.
@@ -0,0 +1,130 @@
1
+ import { Fbq } from "./defineType";
2
+
3
+ type Options = {
4
+ autoConfig?: boolean;
5
+ debug?: boolean;
6
+ };
7
+
8
+ class FacebookPixel {
9
+ pixelId: string;
10
+ autoConfig: boolean;
11
+ initialized: boolean;
12
+
13
+ constructor(pixelId: string, options?: Options) {
14
+ this.pixelId = pixelId;
15
+ this.autoConfig = options?.autoConfig || true;
16
+ this.initialized = false;
17
+ }
18
+
19
+ private loadFacebookPixel() {
20
+ // Verifica se o objeto fbq já está definido no objeto window
21
+ if (window.fbq) return;
22
+
23
+ // Define a função fbq que será usada para enviar eventos ao Facebook Pixel
24
+ const fbq: Fbq = function (...args: any[]) {
25
+ // Se fbq.callMethod estiver definido, chama o método com os argumentos fornecidos
26
+ // Caso contrário, adiciona os argumentos à fila de eventos
27
+ fbq.callMethod ? fbq.callMethod.apply(fbq, args) : fbq.queue.push(args);
28
+ } as Fbq;
29
+
30
+ // Se o objeto _fbq não estiver definido no objeto window, define-o como fbq
31
+ if (!window._fbq) window._fbq = fbq;
32
+
33
+ // Define propriedades e métodos adicionais para fbq
34
+ fbq.push = fbq;
35
+ fbq.loaded = true;
36
+ fbq.version = "2.0";
37
+ fbq.queue = [];
38
+
39
+ // Cria um elemento script e define o atributo src como o URL do script do Facebook Pixel
40
+ const script = document.createElement("script");
41
+ script.async = true;
42
+ script.src = "https://connect.facebook.net/en_US/fbevents.js";
43
+
44
+ // Busca o primeiro elemento script no documento
45
+ const firstScript = document.getElementsByTagName("script")[0];
46
+
47
+ // Estoura uma exceção se não houver nenhum elemento script no documento
48
+ if (!firstScript.parentNode) {
49
+ throw new Error("No script tag found in the document");
50
+ }
51
+
52
+ // Insere o elemento script no início do documento
53
+ firstScript.parentNode.insertBefore(script, firstScript);
54
+ window.fbq = fbq;
55
+ }
56
+
57
+ init(advancedMatching: object = {}) {
58
+ // Verifica se o pixel já foi inicializado
59
+ this.initialized = typeof window !== "undefined" && !!window.fbq;
60
+
61
+ // Carrega o Facebook Pixel
62
+ this.loadFacebookPixel();
63
+
64
+ // Estoura uma exceção se o fbq não estiver definido
65
+ if (!window.fbq) {
66
+ throw new Error("window.fbq is not defined");
67
+ }
68
+
69
+ // Inicializa o pixel com o ID fornecido
70
+ if (this.autoConfig === false) {
71
+ window.fbq("set", "autoConfig", false, this.pixelId);
72
+ } else {
73
+ window.fbq("init", this.pixelId, advancedMatching);
74
+ }
75
+
76
+ // Marca o pixel como inicializado
77
+ this.initialized = true;
78
+ }
79
+
80
+ pageView() {
81
+ if (!this.initialized) return;
82
+ if (!window.fbq) return;
83
+
84
+ window.fbq("track", "PageView");
85
+ }
86
+
87
+ track(title: string, data?: object) {
88
+ if (!this.initialized) return;
89
+ if (!window.fbq) return;
90
+
91
+ window.fbq("track", title, data);
92
+ }
93
+
94
+ trackSingle(pixel: string, title: string, data?: object) {
95
+ if (!this.initialized) return;
96
+ if (!window.fbq) return;
97
+
98
+ window.fbq("trackSingle", pixel, title, data);
99
+ }
100
+
101
+ trackCustom(event: string, data?: object) {
102
+ if (!this.initialized) return;
103
+ if (!window.fbq) return;
104
+
105
+ window.fbq("trackCustom", event, data);
106
+ }
107
+
108
+ trackSingleCustom(pixel: string, event: string, data?: object) {
109
+ if (!this.initialized) return;
110
+ if (!window.fbq) return;
111
+
112
+ window.fbq("trackSingle", pixel, event, data);
113
+ }
114
+
115
+ grantConsent() {
116
+ if (!this.initialized) return;
117
+ if (!window.fbq) return;
118
+
119
+ window.fbq("consent", "grant");
120
+ }
121
+
122
+ revokeConsent() {
123
+ if (!this.initialized) return;
124
+ if (!window.fbq) return;
125
+
126
+ window.fbq("consent", "revoke");
127
+ }
128
+ }
129
+
130
+ export { FacebookPixel };
@@ -2,4 +2,6 @@
2
2
  display: flex;
3
3
  flex-direction: column;
4
4
  gap: 6px;
5
+
6
+ height: max-content;
5
7
  }
package/src/index.ts CHANGED
@@ -65,6 +65,7 @@ export { ToastProvider } from "./provider/ToastProvider";
65
65
 
66
66
  // Others
67
67
  export { ClientOnly } from "./components/ClientOnly";
68
+ export { FacebookPixel } from "./components/FacebookPixel";
68
69
  export { GoogleMap } from "./components/GoogleMap";
69
70
  export { GoogleSearchPlaces } from "./components/GoogleSearchPlaces";
70
71
  export { GoogleTagManager } from "./components/GoogleTagManager";