-
Notifications
You must be signed in to change notification settings - Fork 70
/
integration.js
56 lines (47 loc) · 1.31 KB
/
integration.js
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
// VERSION 0.2.0
// Last Updated: August 30th 2024
const MAX_ATTEMPTS = 9;
const RETRY_DELAY_MS = 1000;
const logEvent = ({campaign, experiment, variation, holdback}) => {
const eventProperties = {
'[Optimizely Campaign]': campaign,
'[Optimizely Experiment]': experiment,
'[Optimizely Variation]': variation,
'[Optimizely Holdback]': holdback
};
window.amplitude.logEvent(extension.event_name, eventProperties);
};
const identifyCall = ({experiment, variation, holdback}) => {
if (!holdback) {
const identifyEvent = new window.amplitude.Identify();
identifyEvent.set(`${extension.property_prefix} ${experiment}`, variation);
window.amplitude.identify(identifyEvent);
}
};
const sendData = () => {
const campaignInfo = window.optimizely
.get('state')
.getDecisionObject({ campaignId });
if (campaignInfo) {
// Always send identify event
identifyCall(campaignInfo);
if (extension.send_event === 'y') {
// Only log event if so configured
logEvent(campaignInfo);
}
}
};
const sendToAmplitude = (numberOfAttempts) => {
if (window.amplitude) {
// Send data to Amplitude
sendData();
} else {
// Retry until Amplitude is initialized
if (numberOfAttempts < MAX_ATTEMPTS) {
setTimeout(() => {
sendToAmplitude(numberOfAttempts + 1);
}, RETRY_DELAY_MS);
}
}
};
sendToAmplitude(0);