File size: 1,946 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
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
import config from '@automattic/calypso-config';
import debug from 'debug';

const mcDebug = debug( 'calypso:analytics:mc' );

function buildQuerystring( group, name ) {
	let uriComponent = '';

	if ( 'object' === typeof group ) {
		for ( const key in group ) {
			uriComponent += '&x_' + encodeURIComponent( key ) + '=' + encodeURIComponent( group[ key ] );
		}
	} else {
		uriComponent = '&x_' + encodeURIComponent( group ) + '=' + encodeURIComponent( name );
	}

	return uriComponent;
}

function buildQuerystringNoPrefix( group, name ) {
	let uriComponent = '';

	if ( 'object' === typeof group ) {
		for ( const key in group ) {
			uriComponent += '&' + encodeURIComponent( key ) + '=' + encodeURIComponent( group[ key ] );
		}
	} else {
		uriComponent = '&' + encodeURIComponent( group ) + '=' + encodeURIComponent( name );
	}

	return uriComponent;
}

export function bumpStat( group, name ) {
	if ( 'object' === typeof group ) {
		mcDebug( 'Bumping stats %o', group );
	} else {
		mcDebug( 'Bumping stat %s:%s', group, name );
	}

	if ( 'undefined' !== typeof window && config( 'mc_analytics_enabled' ) ) {
		const uriComponent = buildQuerystring( group, name );
		new window.Image().src =
			document.location.protocol +
			'//pixel.wp.com/g.gif?v=wpcom-no-pv' +
			uriComponent +
			'&t=' +
			Math.random();
	}
}

export function bumpStatWithPageView( group, name ) {
	// this function is fairly dangerous, as it bumps page views for wpcom and should only be called in very specific cases.
	if ( 'object' === typeof group ) {
		mcDebug( 'Bumping page view with props %o', group );
	} else {
		mcDebug( 'Bumping page view %s:%s', group, name );
	}

	if ( 'undefined' !== typeof window && config( 'mc_analytics_enabled' ) ) {
		const uriComponent = buildQuerystringNoPrefix( group, name );
		new window.Image().src =
			document.location.protocol +
			'//pixel.wp.com/g.gif?v=wpcom' +
			uriComponent +
			'&t=' +
			Math.random();
	}
}