-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathexperimentsankey.js
178 lines (174 loc) · 5.5 KB
/
experimentsankey.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// eslint-disable-next-line import/no-unresolved
import { SankeyController, Flow } from 'chartjs-chart-sankey';
// eslint-disable-next-line import/no-unresolved
import { Chart, registerables } from 'chartjs';
import SankeyChart from './sankey.js';
import { cssVariable, parseConversionSpec } from '../utils.js';
Chart.register(SankeyController, Flow, ...registerables);
const stages = [
{
label: 'contenttype',
/*
* 1. What kind of content was consumed
* In this case, it is just "experiment"
*/
experiment: {
label: (bundle) => bundle.events
.filter((e) => e.checkpoint === 'experiment')
.map((e) => `experiment:${e.source}`)
.pop(),
color: cssVariable('--spectrum-red-800'),
detect: (bundle) => bundle.events
.filter((e) => e.checkpoint === 'experiment')
.length > 0,
next: ['variant:*'],
},
},
{
label: 'variant',
/*
* 2. What variant is selected
* - variant
*/
variant: {
color: cssVariable('--spectrum-fuchsia-300'),
label: (bundle) => bundle.events
.filter((e) => e.checkpoint === 'experiment')
.map((e) => `variant:${e.source} ${e.target}`)
.pop(),
detect: (bundle) => bundle.events
.filter((e) => e.checkpoint === 'experiment')
.length > 0,
next: ['click', 'convert', 'formsubmit', 'nointeraction'],
},
},
{
label: 'interaction',
/*
* 3. What kind of interaction happened
* - convert
* - click
* - formsubmit
* - none
*/
convert: {
color: cssVariable('--spectrum-fuchsia-300'),
label: 'Conversion',
detect: (bundle, dataChunks) => {
const conversionSpec = parseConversionSpec();
if (Object.keys(conversionSpec).length === 0) return false;
if (Object.keys(conversionSpec).length === 1 && conversionSpec.checkpoint && conversionSpec.checkpoint[0] === 'click') return false;
return dataChunks.hasConversion(bundle, conversionSpec, 'every');
},
next: [],
},
click: {
color: cssVariable('--spectrum-green-900'),
label: 'Click',
detect: (bundle) => bundle.events
.filter((e) => e.checkpoint === 'click')
.length > 0,
next: ['intclick', 'extclick', 'media'],
},
formsubmit: {
color: cssVariable('--spectrum-seafoam-900'),
label: 'Form Submit',
detect: (bundle) => bundle.events
.filter((e) => e.checkpoint === 'formsubmit')
.length > 0,
},
nointeraction: {
label: 'No Interaction',
color: cssVariable('--spectrum-gray-100'),
detect: (bundle) => bundle.events
.filter((e) => e.checkpoint === 'click'
|| e.checkpoint === 'formsubmit')
.length === 0,
},
},
{
label: 'clicktarget',
/*
* 4. What's the type of click target
* - external
* - internal
* - media
*/
media: {
color: cssVariable('--spectrum-yellow-1000'),
label: 'Media Click',
next: [],
detect: (bundle) => bundle.events
.filter((e) => e.checkpoint === 'click')
.filter((e) => e.target && e.target.indexOf('media_') > -1)
.length > 0,
},
extclick: {
label: 'External Click',
color: cssVariable('--spectrum-purple-1000'),
next: ['external:*'],
detect: (bundle) => bundle.events
.filter((e) => e.checkpoint === 'click')
.filter((e) => e.target && e.target.startsWith('http'))
.filter((e) => new URL(e.target).hostname !== new URL(bundle.url).hostname)
.length > 0,
},
intclick: {
label: 'Internal Click',
color: cssVariable('--spectrum-green-1000'),
next: ['internal:*'],
detect: (bundle) => bundle.events
.filter((e) => e.checkpoint === 'click')
.filter((e) => !!e.target)
.filter((e) => new URL(e.target).hostname === new URL(bundle.url).hostname)
.length > 0,
},
},
{
label: 'exit',
/*
* 5. What's the click target, specifically
*/
internal: {
color: cssVariable('--spectrum-green-1100'),
next: [],
label: (bundle) => bundle.events.filter((e) => e.checkpoint === 'click')
.filter((e) => !!e.target)
.map((e) => new URL(e.target))
.map((u) => u.pathname)
.map((p) => `internal:${p}`)
.pop(),
detect: (bundle) => bundle.events
.filter((e) => e.checkpoint === 'click')
.filter((e) => !!e.target)
.filter((e) => e.target.indexOf('media_') === -1)
.filter((e) => new URL(e.target).hostname === new URL(bundle.url).hostname)
.length > 0,
},
external: {
color: cssVariable('--spectrum-purple-1100'),
next: [],
label: (bundle) => bundle.events.filter((e) => e.checkpoint === 'click')
.filter((e) => !!e.target)
.map((e) => new URL(e.target))
.map((u) => u.hostname)
.map((h) => `external:${h}`)
.pop(),
detect: (bundle) => bundle.events
.filter((e) => e.checkpoint === 'click')
.filter((e) => e.target)
.filter((e) => e.target.indexOf('media_') === -1)
// only external links for now
.filter((e) => new URL(e.target).hostname !== new URL(bundle.url).hostname)
.length > 0,
},
},
];
const allStages = stages.reduce((acc, stage) => ({ ...acc, ...stage }), {});
export default class ExperimentSankeyChart extends SankeyChart {
constructor(dataChunks, elems) {
super(dataChunks, elems);
this.stages = stages;
this.allStages = allStages;
}
}