-
Notifications
You must be signed in to change notification settings - Fork 2k
/
track-affiliate-referral.js
76 lines (65 loc) · 1.96 KB
/
track-affiliate-referral.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* External dependencies
*/
import { pick } from 'lodash';
import debug from 'debug';
/**
* Internal dependencies
*/
import { recordTracksEvent } from 'lib/analytics/tracks';
const referDebug = debug( 'calypso:analytics:refer' );
const allowedEventProps = [
'status',
'success',
'duplicate',
'description',
'cookie_id',
'vendor_id',
'affiliate_id',
'campaign_id',
'sub_id',
'referrer',
];
export async function trackAffiliateReferral( { affiliateId, campaignId, subId, referrer } ) {
referDebug( 'Recording affiliate referral.', { affiliateId, campaignId, subId, referrer } );
const headers = {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
Accept: 'application/json',
};
const body = new URLSearchParams( {
affiliate_id: affiliateId,
campaign_id: campaignId || '',
sub_id: subId || '',
referrer: referrer || '',
} ).toString();
referDebug( 'Fetching Refer platform response.' );
try {
const response = await window.fetch( 'https://refer.wordpress.com/clicks/67402', {
withCredentials: true, // Needed to check and set the 'wp-affiliate-tracker' cookie.
method: 'POST',
headers,
body,
} );
const json = await response.json();
if ( response.ok ) {
referDebug( 'Recording Refer platform success response.', json );
recordTracksEvent( 'calypso_refer_visit_response', {
...pick( json.data, allowedEventProps ),
status: response.status || '',
success: json.success || true,
description: json.message || 'success',
} );
return;
}
referDebug( 'Recording Refer platform error response.', json );
recordTracksEvent( 'calypso_refer_visit_response', {
...pick( json.data, allowedEventProps ),
status: response.status || '',
success: json.success || false,
description: json.message || 'error',
} );
} catch ( error ) {
// Exception from `fetch` usually means network error. Don't report these to Tracks.
referDebug( 'Failed to fetch Refer platform response.', error );
}
}