-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
223 lines (195 loc) · 8.13 KB
/
index.html
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdn.mxpnl.com/libs/mixpanel-platform/css/reset.css">
<link rel="stylesheet" type="text/css" href="https://cdn.mxpnl.com/libs/mixpanel-platform/build/mixpanel-platform.v0.latest.min.css">
<script src="https://cdn.mxpnl.com/libs/mixpanel-platform/build/mixpanel-platform.v0.latest.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
<style>
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
.node {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: steelblue;
stroke-linecap: round;
}
</style>
</head>
<body class="mixpanel-platform-body" style="overflow-x:scroll;">
<script>
MP.api.ready(function() {
var width = 960;
var height = 750;
var cluster = d3.layout.cluster()
.size([height, width - 160])
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(40,0)");
function addNode(data){
d3.selectAll(".text").remove();
d3.selectAll(".node").remove();
d3.selectAll(".link").remove();
var nodes = cluster.nodes(data),
links = cluster.links(nodes);
nodes.forEach(function(d) { d.y = d.depth * 180; })
var allLinks = svg.selectAll(".link").data(links);
var link = allLinks.enter().append("path")
.attr("class", "link")
.style('stroke-width', function(d) {
return ( d.target.root * .2 );
});
allLinks.transition().duration(1000).attr("d", diagonal);
var allNodes = svg.selectAll(".node").data(nodes);
var node = allNodes.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
.style('cursor','pointer')
.style('opacity', 0.6)
.on('mouseover', function(d){
d3.select(this).style('opacity', 1.0)
d3.select("text").style('opacity', 1.0);
})
.on('mouseout', function(d){
d3.select(this).style('opacity', 0.6)
})
.on('click', function(val){
console.log (val)
if (val.children) {
val._children = val.children;
val.children = null;
addNode(graphData)
} else if (val._children) {
val.children = val._children;
addNode(graphData);
} else if (!ids.has(val.id)) {
ids.add(val.id);
nodeQuery(objs, val);
}
})
node.append('svg:title')
.text(function(d) {
var count = 'Count: ' + String(d.count) + '\n';
var parent = 'Conversion From Parent Event: ' + String(d.conversion) + '%\n';
var root = 'Conversion From Root Event: ' + String(d.root) + '%';
return count + parent + root });
node.append("circle")
.attr("r", 4.5);
node.append("text")
.attr("dx", function(d) { return d.children ? 0 : 8; })
.attr("dy", function(d) { return d.children ? 18 : 3; })
.text(function(d) { return d.name; })
.attr("text-anchor", function(d){
return ( d.children ? "middle" : undefined );
});
}
/* This Event Represents the Origin of the User Flow Diagram. */
var rootEvent = 'user:new';
var graphData = {
'name':rootEvent,
'id': 1,
'conversion': 100,
'root' : 100
};
/* Find the Root Event Count */
var rootEventCount = MP.api.funnel(rootEvent, rootEvent);
$.when(rootEventCount).done(function(eventCount){
var eventTotal = 0;
_.each(_.values(eventCount[0]), function(count){ eventTotal += count.count});
graphData.count = eventTotal;
addNode(graphData);
});
/* Top Event Queries */
var topEventsParams = {'limit':'30'};
var topEvents = MP.api.topEvents(topEventsParams);
/* Global Event List */
var objs = {};
var ids = new Set();
function randomNum() {
return _.random(0, 10000000);
}
/* Funnel Queries */
$.when(topEvents).done(function(events){
/* Cache top events */
objs = events.values();
});
/* Let the Funnel Queries Begin... */
var nodeQuery = function(event_list, node){
/* Dict with top counts for each event */
var nodes = {};
var promises = []
_.each(_.keys(event_list), function(key) {
var event = event_list[key];
var parent;
var events = [];
if (event != node.name) {
/* Build a list of promises */
events.push(event);
events.push(node.name);
if (node.id != graphData.id){ // If this isn't our root event
parent = node.parent;
while (parent.id != graphData.id){
events.push(parent.name);
parent = parent.parent;
}
events.push(rootEvent);
events.reverse();
promises.push(MP.api.funnel.apply(MP.api, events));
} else {
promises.push(MP.api.funnel(node.name, event));
}
}
});
Promise.all(promises).then(function(promiseArray){
_.each(promiseArray, function(data){
total = 0;
var length = data.length - 1;
_.each(_.values(data[length]), function(funnelObj){ total += funnelObj.count });
nodes[_.values(data[length])[0].event] = total;
});
}).then(function(){
/* Add Data to Graph Data */
var total = _.values(nodes).reduce(function(a , b){return a + b;});
_.each(_.keys(nodes), function(key){
/*
Build a node object with the conversion percentage and other meta data and add it to the children
of the parent node
*/
var rando = randomNum();
var conversion = Math.floor(nodes[key] / node.count * 100);
var root = Math.floor(nodes[key] / graphData.count * 100);
if (conversion < 2 ) { return };
if (node.children) {
node.children.push({
'name':key,
'id':rando,
'root': root,
'conversion': conversion,
'count':nodes[key]
});
} else {
node.children = [{
'name':key,
'id':rando,
'root': root,
'conversion': conversion,
'count':nodes[key]
}];
}
})
addNode(graphData);
});
}
});
</script>
</body>
</html>