{"version":3,"sources":["../src/setup.ts","../src/index.ts","../src/types/index.ts"],"sourcesContent":["import type { Env } from './types';\nimport { getEnv } from '@walkeros/web-core';\n\nexport function addScript(\n  env?: Env,\n  src = 'https://connect.facebook.net/en_US/fbevents.js',\n) {\n  const { document } = getEnv<Env>(env);\n  const script = document.createElement('script');\n  script.src = src;\n  script.async = true;\n  document.head.appendChild(script);\n}\n\n/**\n * The fbevents.js bootstrap stub: a callable that queues calls until the real\n * library loads and installs `callMethod`. The intersection captures both the\n * public `fbq` call signature and the internal queue properties the snippet\n * mutates.\n */\ntype FbqStub = facebook.Pixel.Event & {\n  callMethod?: (...args: unknown[]) => void;\n  queue: unknown[];\n  push?: facebook.Pixel.Event;\n  loaded?: boolean;\n  version?: string;\n};\n\nexport function setup(env?: Env) {\n  const { window } = getEnv<Env>(env);\n  if (window.fbq) return;\n\n  const fbq: FbqStub = Object.assign(\n    function (...args: unknown[]): void {\n      fbq.callMethod ? fbq.callMethod.apply(fbq, args) : fbq.queue.push(args);\n    },\n    { queue: [] },\n  );\n  fbq.push = fbq;\n  fbq.loaded = true;\n  fbq.version = '2.0';\n\n  window.fbq = fbq;\n  if (!window._fbq) window._fbq = fbq;\n}\n","import type { Destination, Env } from './types';\nimport { addScript, setup } from './setup';\nimport { isObject } from '@walkeros/core';\nimport { getEnv } from '@walkeros/web-core';\n\n// Types\nexport * as DestinationMeta from './types';\n\nexport const destinationMeta: Destination = {\n  type: 'meta-pixel',\n\n  config: {},\n\n  init({ config, env, logger }) {\n    const { settings, loadScript } = config;\n    const { pixelId } = settings || {};\n\n    // Load Meta Pixel script if required (fbevents.js)\n    if (loadScript) addScript(env);\n\n    // Required pixel id\n    if (!pixelId) logger.throw('Config settings pixelId missing');\n\n    // fbq function setup\n    setup(env);\n\n    const { window } = getEnv<Env>(env);\n    window.fbq!('init', pixelId!);\n  },\n\n  push(event, { config, rule = {}, data, env }) {\n    const { track, trackCustom } = rule.settings || {};\n    const { window } = getEnv<Env>(env);\n    const fbq = window.fbq!;\n\n    // page view\n    if (event.name === 'page view' && !rule.settings) {\n      // Define a custom mapping\n      event.name = 'PageView';\n    }\n\n    const eventName = track || trackCustom || event.name;\n\n    fbq(\n      trackCustom ? 'trackCustom' : 'track',\n      String(eventName),\n      isObject(data) ? data : {},\n      { eventID: event.id },\n    );\n  },\n};\n\nexport default destinationMeta;\n","import type {\n  Mapping as WalkerOSMapping,\n  Destination as CoreDestination,\n} from '@walkeros/core';\nimport type { DestinationWeb } from '@walkeros/web-core';\n\ndeclare global {\n  interface Window {\n    _fbq?: facebook.Pixel.Event;\n    fbq?: facebook.Pixel.Event;\n  }\n}\n\nexport interface Settings {\n  pixelId: string;\n}\n\nexport type InitSettings = Partial<Settings>;\n\nexport interface Mapping {\n  track?: StandardEventNames;\n  trackCustom?: string;\n}\n\nexport interface Env extends DestinationWeb.Env {\n  window: {\n    // Optional: the SDK global is absent until `setup` installs the stub.\n    fbq?: facebook.Pixel.Event;\n    _fbq?: facebook.Pixel.Event;\n  };\n  document: {\n    createElement: (tagName: string) => {\n      src: string;\n      async: boolean;\n      setAttribute: (name: string, value: string) => void;\n      removeAttribute: (name: string) => void;\n    };\n    head: { appendChild: (node: unknown) => void };\n  };\n}\n\nexport type Types = CoreDestination.Types<Settings, Mapping, Env, InitSettings>;\n\nexport type Destination = DestinationWeb.Destination<Types>;\nexport type Config = DestinationWeb.Config<Types>;\n\nexport type Rule = WalkerOSMapping.Rule<Mapping>;\nexport type Rules = WalkerOSMapping.Rules<Rule>;\n\nexport type StandardEventNames =\n  | 'PageView'\n  | 'AddPaymentInfo'\n  | 'AddToCart'\n  | 'AddToWishlist'\n  | 'CompleteRegistration'\n  | 'Contact'\n  | 'CustomizeProduct'\n  | 'Donate'\n  | 'FindLocation'\n  | 'InitiateCheckout'\n  | 'Lead'\n  | 'Purchase'\n  | 'Schedule'\n  | 'Search'\n  | 'StartTrial'\n  | 'SubmitApplication'\n  | 'Subscribe'\n  | 'ViewContent'\n  | string;\n"],"mappings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