File size: 1,650 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
import { recordTracksEvent } from './tracks';

export type Railcar = {
	railcar: string;
	fetch_algo: string;
	fetch_lang: string;
	fetch_position: number;
	fetch_query?: string;
	rec_blog_id: string;
} & Record< string, string | number >;

interface TrainTracksRenderProps {
	railcarId: string;
	uiAlgo: string;
	uiPosition: number;
	fetchAlgo: string;
	fetchPosition?: number;
	result?: string;
	query?: string;
	recBlogId?: string;
	recPostId?: string;
	recFeedId?: string;
	recFeedItemId?: string;
}

interface TrainTracksInteractProps {
	railcarId: string;
	action: string;
}

export function recordTrainTracksRender( {
	railcarId,
	uiAlgo,
	uiPosition,
	fetchAlgo,
	fetchPosition,
	query,
	result,
	recBlogId,
	recPostId,
	recFeedId,
	recFeedItemId,
}: TrainTracksRenderProps ) {
	const props: { [ key: string ]: string | number } = {};

	// Remap and filter undefined props
	Object.entries( {
		railcar: railcarId,
		ui_algo: uiAlgo,
		ui_position: uiPosition,
		fetch_algo: fetchAlgo,
		fetch_query: query,
		fetch_position: fetchPosition,
		rec_result: result,
		rec_blog_id: recBlogId,
		rec_post_id: recPostId,
		rec_feed_id: recFeedId,
		rec_feed_item_id: recFeedItemId,
	} ).forEach( ( [ key, val ] ) => val !== undefined && ( props[ key ] = val ) );

	recordTracksEvent( 'calypso_traintracks_render', props );
}

export function recordTrainTracksInteract( { railcarId, action }: TrainTracksInteractProps ) {
	recordTracksEvent( 'calypso_traintracks_interact', {
		railcar: railcarId,
		action,
	} );
}

export function getNewRailcarId( suffix = 'recommendation' ) {
	return `${ crypto.randomUUID().replace( /-/g, '' ) }-${ suffix }`;
}