-
Notifications
You must be signed in to change notification settings - Fork 0
/
optisizely.js
188 lines (173 loc) · 6.46 KB
/
optisizely.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
179
180
181
182
183
184
185
186
187
188
$(function () {
var experiments = [];
var variations = [];
var totalArea;
var optimizelyId = window.location.href.split('?')[1]
if (!optimizelyId) {
return;
}
$.getScript('https://cdn.optimizely.com/js/' + optimizelyId + '.js', function () {
experiments = optimizely.allExperiments;
variations = optimizely.allVariations;
// Attach extra metadata onto optimizely.allVariations.
// Calculate the approximate size of each variation.
$.each(variations, function (variationId) {
var variation = variations[variationId];
variation.variationId = variationId;
variation.size = JSON.stringify(variation.code||'').length;
});
// Attach extra metadata onto optimizely.allExperiments.
// Link each experiment to its variations.
// Calculate the total size of each experiment.
$.each(experiments, function (experimentKey, experiment) {
experiment.variations = experiment.variation_ids.map(function (variationId) {
variations[variationId].experimentKey = experimentKey;
variations[variationId].weight = (experiment.variation_weights||[])[variationId];
return variations[variationId];
});
experiment.experimentKey = experimentKey;
experiment.size = JSON.stringify(experiment).length;
});
totalArea = JSON.stringify(experiments).length;
buildTree();
});
// Some templates for the block contents
function bToKb (b) {
return (b / 1024).toFixed(1) + 'KB';
}
function variationHtml (variation) {
var notRunning = !variation.weight;
var running100 = (variation.weight === 10000);
var className = (notRunning || running100) ? 'warning' : '';
return '<p class="' + className + '">' +
'<a data-variation-id="' + variation.variationId + '">' +
bToKb(variation.size) +
'</a> ' +
'Variation ' + variation.variationId +
(notRunning ? ' <strong>NOT RUNNING</strong>' : '') +
(running100 ? ' <strong>100% WEIGHT</strong>' : '') +
'</p>';
}
function experimentHtml (experiment) {
return '<p>' +
'<strong>' +
'<a data-variation-id="' + experiment.variations[experiment.variations.length-1].variationId + '">' +
(100*experiment.size/totalArea).toFixed(1) + '% ' +
bToKb(experiment.size) +
'</a> ' +
experiment.name +
' (<a target="_blank" ' +
'href="https://app.optimizely.com/projects/' + optimizelyId + '/experiments/' + experiment.experimentKey + '?search=' + experiment.experimentKey + '">' +
experiment.experimentKey +
'</a>)' +
'</strong>' +
'</p>' +
experiment.variations.map(variationHtml).join('');
}
function experimentUrlHtml (experiment) {
if (!experiment.urls) return '';
return '<p><strong>Url targeting</strong></p>' +
'<ul>' +
experiment.urls.map(function (url) {
return '<li>' + url.match + ': ' + url.value + '</li>'
}).join('') +
'</ul>';
}
// Build the treemap.
// Each kTree node is an object containing {name, data, and children}.
// Refer to the webtreemap documentation somewhere online.
function buildTree () {
var sortby = $('#sortby').val();
var sortFn = {
age: function (a, b) { return a.data.experiment.experimentKey - b.data.experiment.experimentKey; },
size: function (a, b) { return b.data.experiment.size - a.data.experiment.size; },
}[sortby];
var kTree = {
name: "<b>Optimizely experiments and variations " + bToKb(totalArea) + "</b>",
data: { '$area': totalArea },
children: $.map(experiments, function (experiment) {
var warning = (experiment.variations||[]).some(function (variation) {
return variation.weight === 10000;
}) || (experiment.variations||[]).every(function (variation) {
return !variation.weight;
});
var lightWarning = (experiment.variations||[]).some(function (variation) {
return !variation.weight;
});
return {
name: experimentHtml(experiment),
data: {
'$area': experiment.size,
experiment: experiment,
'$symbol': warning ? 'warning' : lightWarning ? 'light-warning' : ''
}
}
}).sort(sortFn)
}
appendTreemap($('#map').empty().get(0), kTree);
$('#list').html(kTree.children.map(function (experiment) {
return '<li>'+experiment.name+'</li>';
}).join(''));
}
function debounce (fn, time) {
var timeout;
return function() {
clearTimeout(timeout);
timeout = setTimeout(fn, time);
}
}
$(window).on('resize', debounce(buildTree, 500));
$('#sortby').on('change', buildTree);
$('body').on('click', '[data-variation-id]', function () {
var variation = variations[$(this).data('variation-id')];
var experiment = experiments[variation.experimentKey];
var source = variation.code || '';
codemirror.setValue(source);
$('.current-variation').html(
experimentHtml(experiment) + '<hr>' +
experimentUrlHtml(experiment) + '<hr>' +
variationHtml(variation)
);
$('#list-pane').css('transform', 'translateX(-100%)');
$('#map-pane').css('transform', 'translateX(-100%)');
$('#code').css('transform', 'translateX(0)');
});
$('.back-to-map').on('click', function () {
$('#list-pane').css('transform', 'translateX(-100%)');
$('#map-pane').css('transform', 'translateX(0)');
$('#code').css('transform', 'translateX(100%)');
});
$('.back-to-list').on('click', function () {
$('#list-pane').css('transform', 'translateX(0)');
$('#map-pane').css('transform', 'translateX(100%)');
$('#code').css('transform', 'translateX(100%)');
});
var codemirror = CodeMirror.fromTextArea($('#source')[0], {
theme: 'default',
lineNumbers: true,
lineWrapping: true
});
});
// Needs to be global for codemirror ¯\_(ツ)_/¯
function beautify(source) {
var output;
var opts = {
indent_size: 2,
indent_char: ' ',
max_preserve_newlines: 5,
preserve_newlines: true,
keep_array_indentation: false,
break_chained_methods: false,
indent_scripts: 'normal',
brace_style: 'collapse',
space_before_conditional: true,
unescape_strings: false,
jslint_happy: false,
end_with_newline: false,
wrap_line_length: '0',
indent_inner_html: false,
comma_first: false,
e4x: false
};
return js_beautify(source, opts);
}