-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathindex.jsx
120 lines (109 loc) · 3.33 KB
/
index.jsx
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* eslint-disable no-console */
import { useContext, useCallback, useState } from 'react';
import { path } from 'rambda';
import { pathOr } from 'rambda';
import { EventTrackingContext } from '../../contexts/EventTrackingContext';
import useTrackingToggle from '../useTrackingToggle';
import OPTIMIZELY_CONFIG from '../../lib/config/optimizely';
import { sendEventBeacon } from '../../components/ATIAnalytics/beacon/index';
import { ServiceContext } from '../../contexts/ServiceContext';
import { isValidClick } from './clickTypes';
const EVENT_TYPE = 'click';
const useClickTrackerHandler = (props = {}) => {
const preventNavigation = path(['preventNavigation'], props);
const componentName = path(['componentName'], props);
const url = path(['url'], props);
const advertiserID = path(['advertiserID'], props);
const format = path(['format'], props);
const optimizely = path(['optimizely'], props);
const detailedPlacement = props?.detailedPlacement;
const { trackingIsEnabled } = useTrackingToggle(componentName);
const [clicked, setClicked] = useState(false);
const eventTrackingContext = useContext(EventTrackingContext);
const { pageIdentifier, platform, producerId, statsDestination } =
eventTrackingContext;
const campaignID = pathOr(
path(['campaignID'], eventTrackingContext),
['campaignID'],
props,
);
const { service } = useContext(ServiceContext);
return useCallback(
async event => {
const shouldRegisterClick = [
trackingIsEnabled,
!clicked,
isValidClick(event),
].every(Boolean);
if (shouldRegisterClick) {
setClicked(true);
const shouldSendEvent = [
campaignID,
componentName,
pageIdentifier,
platform,
producerId,
service,
statsDestination,
].every(Boolean);
if (shouldSendEvent) {
const nextPageUrl = path(['currentTarget', 'href'], event);
event.stopPropagation();
event.preventDefault();
if (optimizely) {
const overrideAttributes = {
...optimizely.user.attributes,
[`clicked_${OPTIMIZELY_CONFIG.viewClickAttributeId}`]: true,
};
optimizely.track(
'component_clicks',
optimizely.user.id,
overrideAttributes,
);
}
try {
await sendEventBeacon({
type: EVENT_TYPE,
campaignID,
componentName,
format,
pageIdentifier,
platform,
producerId,
service,
advertiserID,
statsDestination,
url,
detailedPlacement,
});
} finally {
if (nextPageUrl && !preventNavigation) {
if (optimizely) {
optimizely.close();
}
window.location.assign(nextPageUrl);
}
}
}
}
},
[
trackingIsEnabled,
clicked,
campaignID,
componentName,
pageIdentifier,
platform,
preventNavigation,
producerId,
service,
statsDestination,
url,
advertiserID,
format,
optimizely,
detailedPlacement,
],
);
};
export default useClickTrackerHandler;