2
0

(pixel) Add skip initialization option

This commit is contained in:
Baptiste Arnaud
2023-06-28 10:29:58 +02:00
parent 033f8f99dd
commit 50fcbfd95e
12 changed files with 822 additions and 276 deletions

View File

@@ -1,4 +1,4 @@
import { isEmpty } from '@typebot.io/lib/utils'
import { isDefined, isEmpty } from '@typebot.io/lib/utils'
import type { GoogleAnalyticsOptions } from '@typebot.io/schemas'
declare const gtag: (
@@ -12,8 +12,9 @@ declare const gtag: (
}
) => void
export const initGoogleAnalytics = (id: string): Promise<void> =>
new Promise((resolve) => {
export const initGoogleAnalytics = (id: string): Promise<void> => {
if (isDefined(gtag)) return Promise.resolve()
return new Promise((resolve) => {
const existingScript = document.getElementById('gtag')
if (!existingScript) {
const script = document.createElement('script')
@@ -34,6 +35,7 @@ export const initGoogleAnalytics = (id: string): Promise<void> =>
}
if (existingScript) resolve()
})
}
export const sendGaEvent = (options: GoogleAnalyticsOptions) => {
if (!options) return

View File

@@ -3,7 +3,8 @@ import { PixelBlock } from '@typebot.io/schemas'
declare const fbq: (
arg0: string,
arg1: string,
arg2: Record<string, string> | undefined
arg2: string,
arg3: Record<string, string> | undefined
) => void
export const initPixel = (pixelId: string) => {
@@ -26,7 +27,7 @@ export const initPixel = (pixelId: string) => {
}
export const trackPixelEvent = (options: PixelBlock['options']) => {
if (!options.eventType) return
if (!options.eventType || !options.pixelId) return
const params = options.params?.length
? options.params.reduce<Record<string, string>>((obj, param) => {
if (!param.key || !param.value) return obj
@@ -35,7 +36,7 @@ export const trackPixelEvent = (options: PixelBlock['options']) => {
: undefined
if (options.eventType === 'Custom') {
if (!options.name) return
fbq('trackCustom', options.name, params)
fbq('trackCustom', options.pixelId, options.name, params)
}
fbq('track', options.eventType, params)
fbq('track', options.pixelId, options.eventType, params)
}