|
1 | 1 | <script>
|
2 |
| - export let id |
| 2 | + import { onMount } from 'svelte' |
| 3 | + import loader from '@beyonk/async-script-loader' |
| 4 | + import { queue } from './queue.js' |
| 5 | +
|
3 | 6 | export let enabled = true
|
4 |
| - export let version = 'v3.1' |
5 |
| - let pixels |
| 7 | + export let pixels = [] |
| 8 | +
|
6 | 9 | let _fbq
|
7 | 10 |
|
8 |
| - export function enable () { |
9 |
| - mount() |
| 11 | + onMount(() => { |
| 12 | + if (!enabled) { return } |
10 | 13 | init()
|
11 |
| - track() |
| 14 | + }) |
| 15 | +
|
| 16 | + export function init () { |
| 17 | + loader( |
| 18 | + [ |
| 19 | + { |
| 20 | + type: 'script', |
| 21 | + url: 'https://connect.facebook.net/en_US/fbevents.js' |
| 22 | + } |
| 23 | + ], |
| 24 | + test, |
| 25 | + callback |
| 26 | + ) |
12 | 27 | }
|
13 | 28 |
|
14 |
| - function mount () { |
15 |
| - if (window['fbq']) { |
16 |
| - _fbq = window['fbq'] |
17 |
| - return |
18 |
| - } |
| 29 | + function test () { |
| 30 | + return Boolean(window.dataLayer).valueOf() && Array.isArray(window.dataLayer) |
| 31 | + } |
19 | 32 |
|
20 |
| - if (!id) { |
21 |
| - throw new Error( |
22 |
| - `id configuration parameter is required, try <FacebookPixel id="12345" /> or <FacebookPixel id={['12345', '67890']} /> for multiple pixels` |
23 |
| - ) |
24 |
| - } |
| 33 | + function query (cmd, params) { |
| 34 | + _fbq(cmd, ...params) |
| 35 | + } |
25 | 36 |
|
26 |
| - pixels = Array.isArray(id) ? id : [ id ] |
| 37 | + function callback () { |
| 38 | + if (!window.fbq) { return } |
27 | 39 |
|
28 |
| - /* eslint-disable */ |
29 |
| - const scr = (f, b, e, v, n, t, s) => { |
30 |
| - if (f.fbq) return; n = f.fbq = function (...args) { |
31 |
| - n.callMethod ? |
32 |
| - n.callMethod.apply(n, args) : n.queue.push(args) |
33 |
| - }; |
34 |
| - if (!f._fbq) f._fbq = n; n.push = n; n.loaded = !0; n.version = version; |
35 |
| - n.queue = []; |
36 |
| - t = b.createElement(e); |
37 |
| - t.async = true; |
38 |
| - t.defer = true; |
39 |
| - t.src = v; |
40 |
| - s = b.getElementsByTagName('body')[0]; |
41 |
| - s.parentNode.appendChild(t, s); |
42 |
| - } |
43 |
| - |
44 |
| - scr(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js') |
45 |
| - /* eslint-enable */ |
| 40 | + pixels.forEach(pixel => query('init', [pixel])) |
46 | 41 |
|
47 |
| - _fbq = window['fbq'] |
48 |
| - |
49 |
| - if (enabled) { |
50 |
| - enable() |
51 |
| - } |
52 |
| - } |
| 42 | + return queue.subscribe(queue => { |
| 43 | + let next = queue.length && queue.shift() |
53 | 44 |
|
54 |
| - function init () { |
55 |
| - pixels.forEach(id => { |
56 |
| - query('init', id) |
57 |
| - }) |
58 |
| - } |
59 |
| -
|
60 |
| - export function track (event = 'PageView', data = undefined, id = undefined) { |
61 |
| - if (id) { |
62 |
| - if (pixels.includes(id)) { |
63 |
| - query('trackSingle', id, event, data) |
64 |
| - } else { |
65 |
| - console.warn('Attempted to send event to unknown pixel', id) |
| 45 | + while (next) { |
| 46 | + const { type, params } = next |
| 47 | + query(type, params) |
| 48 | + next = queue.shift() |
66 | 49 | }
|
67 |
| - } else { |
68 |
| - query('track', event, data) |
69 |
| - } |
70 |
| - } |
71 |
| -
|
72 |
| - export function query (cmd, ...option) { |
73 |
| - _fbq(cmd, ...option) |
| 50 | + }) |
74 | 51 | }
|
75 | 52 | </script>
|
0 commit comments