-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconversion-tracking.js
More file actions
104 lines (85 loc) · 2.46 KB
/
Copy pathconversion-tracking.js
File metadata and controls
104 lines (85 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const initConversionTracking = () => {
const {
a: API_HOST,
k: PUBLISHABLE_KEY,
c: cookieManager,
i: DUB_ID_VAR,
} = window._dubAnalytics || {};
if (!API_HOST) {
console.warn('[dubAnalytics] Missing API_HOST');
return;
}
if (!PUBLISHABLE_KEY) {
console.warn('[dubAnalytics] Missing PUBLISHABLE_KEY');
return;
}
// Track lead conversion
const trackLead = async (input) => {
const clickId = cookieManager?.get(DUB_ID_VAR);
const requestBody = {
...(clickId && { clickId }),
...input,
};
const response = await fetch(`${API_HOST}/track/lead/client`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${PUBLISHABLE_KEY}`,
},
body: JSON.stringify(requestBody),
});
const result = await response.json();
if (!response.ok) {
console.error('[dubAnalytics] trackLead failed', result.error);
}
return result;
};
// Track sale conversion
const trackSale = async (input) => {
const response = await fetch(`${API_HOST}/track/sale/client`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${PUBLISHABLE_KEY}`,
},
body: JSON.stringify(input),
});
const result = await response.json();
if (!response.ok) {
console.error('[dubAnalytics] trackSale failed', result.error);
}
return result;
};
// Add methods to the global dubAnalytics object for direct calls
if (window.dubAnalytics) {
window.dubAnalytics.trackLead = function (...args) {
trackLead(...args);
};
window.dubAnalytics.trackSale = function (...args) {
trackSale(...args);
};
}
// Process any existing queued conversion events
if (window._dubAnalytics && window._dubAnalytics.qm) {
const queueManager = window._dubAnalytics.qm;
const existingQueue = queueManager.queue || [];
const remainingQueue = existingQueue.filter(([method, ...args]) => {
if (method === 'trackLead') {
trackLead(...args);
return false;
} else if (method === 'trackSale') {
trackSale(...args);
return false;
}
return true;
});
// Update the queue with remaining items
queueManager.queue = remainingQueue;
}
};
// Run when base script is ready
if (window._dubAnalytics) {
initConversionTracking();
} else {
window.addEventListener('load', initConversionTracking);
}