File size: 2,663 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
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
import crypto from 'crypto';
import { type Request } from 'express';
import superagent from 'superagent';
const URL = require( 'url' );

interface UserParameters {
	_ul?: string;
	_ui: string | number;
	_ut: string;
}

function getUserFromRequest( request: Request ): UserParameters {
	// if user has a cookie, lets use that
	const encodedUserCookie = request?.cookies?.wordpress_logged_in ?? null;

	if ( encodedUserCookie ) {
		try {
			const userCookieParts = decodeURIComponent( encodedUserCookie ).split( '|' );

			// We don't trust it, but for analytics this is enough
			return {
				_ul: userCookieParts[ 0 ],
				_ui: parseInt( userCookieParts[ 1 ], 10 ),
				_ut: 'wpcom:user_id',
			};
		} catch ( ex ) {
			// ignore the error and let it fallback to anonymous below
		}
	}

	// if user doesn't have a cookie, and we had the user identification passed to us on query params
	// we'll use that, otherwise, we'll just use anonymous.

	// If we have a full identity on query params - use it
	if ( request?.query?._ut && request?.query?._ui ) {
		return {
			_ui: request.query._ui as string,
			_ut: request.query._ut as string,
		};
	}

	// We didn't get a full identity, create an anon ID
	return {
		_ut: 'anon',
		_ui: crypto.randomUUID(),
	};
}

const analytics = {
	tracks: {
		createPixel: function ( data: Record< string, string | number | undefined > ) {
			data._rt = new Date().getTime();
			data._ = '_';
			const pixelUrl = URL.format( {
				protocol: 'http',
				host: 'pixel.wp.com',
				pathname: '/t.gif',
				query: data,
			} );
			superagent.get( pixelUrl ).end();
		},

		recordEvent: function (
			eventName: string,
			eventProperties: Record< string, string | undefined >,
			req: Request
		) {
			eventProperties = eventProperties || {};

			if ( eventName.indexOf( 'calypso_' ) !== 0 ) {
				console.warn( '- Event name must be prefixed by "calypso_"' );
				return;
			}

			// Remove properties that have an undefined value
			// This allows a caller to easily remove properties from the recorded set by setting them to undefined
			eventProperties = Object.fromEntries(
				Object.entries( eventProperties ).filter( ( entry ) => entry[ 1 ] !== undefined )
			);

			const date = new Date();
			const acceptLanguageHeader = req.get( 'Accept-Language' ) || '';

			this.createPixel( {
				_en: eventName,
				_ts: date.getTime(),
				_dl: req.get( 'Referer' ),
				_lg: acceptLanguageHeader.split( ',' )[ 0 ],
				_via_ip: req.get( 'x-forwarded-for' ) || req.connection.remoteAddress,
				_via_ua: req.get( 'user-agent' ),
				...getUserFromRequest( req ),
				...eventProperties,
			} );
		},
	},
};
export default analytics;