|
|
import debug from 'debug'; |
|
|
import { pick } from 'lodash'; |
|
|
import { recordTracksEvent } from 'calypso/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( { |
|
|
vendorId, |
|
|
affiliateId, |
|
|
campaignId, |
|
|
subId, |
|
|
referrer, |
|
|
} ) { |
|
|
referDebug( 'Recording affiliate referral.', { |
|
|
vendorId, |
|
|
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/${ vendorId }`, { |
|
|
credentials: 'include', |
|
|
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 ) { |
|
|
|
|
|
referDebug( 'Failed to fetch Refer platform response.', error ); |
|
|
} |
|
|
} |
|
|
|