File size: 828 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cookie from 'cookie';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import urlParseAmpCompatible from './utils/url-parse-amp-compatible';

/**
 * Save Impact Affiliate Click ID in a cookie if it is set as a query parameter.
 */
export default function saveImpactAffiliateClickId() {
	// Read click ID query argument, return early if there is none.
	const parsedUrl = urlParseAmpCompatible( window.location.href );
	const clickId = parsedUrl?.searchParams.get( 'irclickid' );

	if ( ! clickId ) {
		return;
	}

	document.cookie = cookie.serialize( 'irclickid', clickId, {
		path: '/',
		expires: new Date( Date.now() + 30 * 24 * 60 * 60 * 1000 ), // 30 days
		domain: '.wordpress.com',
	} );

	recordTracksEvent( 'calypso_impact_affiliate_visit', {
		page: parsedUrl.host + parsedUrl.pathname,
	} );
}