1 (function (angular
, $, _
) {
3 var partialUrl = function (relPath
) {
4 return CRM
.resourceUrls
['civicrm'] + '/partials/crmMailingAB/' + relPath
;
6 var crmMailingAB
= angular
.module('crmMailingAB');
8 crmMailingAB
.controller('ReportCtrl', function ($scope
, crmApi
, selectedABTest
, $location
) {
17 $scope
.graphload
= false;
18 if (selectedABTest
.winner_criteria_id
== 1) {
19 $scope
.winnercriteria
= "Open";
22 if (selectedABTest
.winner_criteria_id
== 2) {
23 $scope
.winnercriteria
= "Total Unique Clicks";
26 if (selectedABTest
.winner_criteria_id
== 3) {
27 $scope
.winnercriteria
= "Total Clicks on a particular link";
32 $scope
.copyatoc = function () {
33 var res
= crmApi('Mailing', 'get', {id
: selectedABTest
.mailing_id_a
});
34 res
.success(function (data
) {
35 for (var key
in data
.values
) {
36 var mail
= data
.values
[key
];
38 mail
.id
= selectedABTest
.mailing_id_c
;
39 crmApi('Mailing', 'create', mail
);
41 $location
.path('abtest');
44 $scope
.sendc = function () {
45 var res
= crmApi('Mailing', 'get', {id
: selectedABTest
.mailing_id_b
});
46 res
.success(function (data
) {
47 for (var key
in data
.values
) {
48 var mail
= data
.values
[key
];
50 mail
.id
= selectedABTest
.mailing_id_c
;
51 crmApi('Mailing', 'create', mail
);
53 $location
.path('abtest');
55 var result
= crmApi('Mailing', 'stats', {mailing_id
: selectedABTest
.mailing_id_a
});
57 result
.success(function (data
) {
59 $scope
.r
.push(data
.values
[selectedABTest
.mailing_id_a
]["Delivered"].toString());
60 $scope
.r
.push(data
.values
[selectedABTest
.mailing_id_a
]["Bounces"].toString());
61 $scope
.r
.push(data
.values
[selectedABTest
.mailing_id_a
]["Unsubscribers"].toString());
62 $scope
.r
.push(data
.values
[selectedABTest
.mailing_id_a
]["Opened"].toString());
63 $scope
.r
.push(data
.values
[selectedABTest
.mailing_id_a
]["Unique Clicks"].toString());
68 result
= crmApi('Mailing', 'stats', {mailing_id
: selectedABTest
.mailing_id_b
});
69 result
.success(function (data
) {
70 $scope
.d
.push(data
.values
[selectedABTest
.mailing_id_b
]["Delivered"].toString());
71 $scope
.d
.push(data
.values
[selectedABTest
.mailing_id_b
]["Bounces"].toString());
72 $scope
.d
.push(data
.values
[selectedABTest
.mailing_id_b
]["Unsubscribers"].toString());
73 $scope
.d
.push(data
.values
[selectedABTest
.mailing_id_b
]["Opened"].toString());
74 $scope
.d
.push(data
.values
[selectedABTest
.mailing_id_b
]["Unique Clicks"].toString());
77 $scope
.aastop
= false;
78 $scope
.asure = function () {
81 $scope
.bbstop
= false;
82 $scope
.bsure = function () {
88 for (i
= 1; i
<= numdiv
; i
++) {
89 var result
= crmApi('MailingAB', 'graph_stats', {id
: selectedABTest
.id
, split_count
: numdiv
, split_count_select
: i
});
90 result
.success(function (data
) {
93 for (var key
in data
.values
.A
) {
96 var t
= data
.values
.A
[temp
].time
.split(" ");
99 var day
= t
[1].substr(0, t
[1].length
- 3);
101 var t1
= t
[4].split(":");
110 hour
= parseInt(hur
) + 12;
115 var t1
= t
[3].split(":");
124 hour
= parseInt(hur
) + 12;
168 var tp
= new Date(year
, month
, day
, hour
, min
, 0, 0);
169 $scope
.graph_data
[temp
- 1] = {
171 x
: data
.values
.A
[temp
].count
,
172 y
: data
.values
.B
[temp
].count
175 if (keep_cnt
== numdiv
) {
176 $scope
.graphload
= true;
178 var data
= $scope
.graph_data
;
180 // set up a colour variable
181 var color
= d3
.scale
.category10();
183 // map one colour each to x, y and z
184 // keys grabs the key value or heading of each key value pair in the json
186 color
.domain(d3
.keys(data
[0]).filter(function (key
) {
187 return key
!== "time";
190 // create a nested series for passing to the line generator
191 // it's best understood by console logging the data
192 var series
= color
.domain().map(function (name
) {
195 values
: data
.map(function (d
) {
204 // Set the dimensions of the canvas / graph
211 width
= 550 - margin
.left
- margin
.right
,
212 height
= 350 - margin
.top
- margin
.bottom
;
215 //var x = d3.time.scale().range([0, width]).domain([0,10]);
216 var x
= d3
.time
.scale().range([0, width
]);
217 var y
= d3
.scale
.linear().range([height
, 0]);
220 var xAxis
= d3
.svg
.axis().scale(x
)
221 .orient("bottom").ticks(10);
223 var yAxis
= d3
.svg
.axis().scale(y
)
224 .orient("left").ticks(5);
227 // Note you plot the time / score pair from each key you created ealier
228 var valueline
= d3
.svg
.line()
236 // Adds the svg canvas
237 var svg
= d3
.select("#linegraph")
239 .attr("width", width
+ margin
.left
+ margin
.right
)
240 .attr("height", height
+ margin
.top
+ margin
.bottom
)
242 .attr("transform", "translate(" + margin
.left
+ "," + margin
.top
+ ")");
244 // Scale the range of the data
245 x
.domain(d3
.extent(data
, function (d
) {
249 // note the nested nature of this you need to dig an additional level
251 d3
.min(series
, function (c
) {
252 return d3
.min(c
.values
, function (v
) {
256 d3
.max(series
, function (c
) {
257 return d3
.max(c
.values
, function (v
) {
262 svg
.append("text") // text label for the x axis
263 .attr("x", width
/ 2)
264 .attr("y", height
+ margin
.bottom
)
265 .style("text-anchor", "middle")
268 svg
.append("text") // text label for the x axis
269 .style("text-anchor", "middle")
270 .text($scope
.winnercriteria
).attr("transform",function (d
) {
272 }).attr("x", -height
/ 2)
275 // create a variable called series and bind the date
276 // for each series append a g element and class it as series for css styling
277 var series
= svg
.selectAll(".series")
280 .attr("class", "series");
282 // create the path for each series in the variable series i.e. x, y and z
283 // pass each object called x, y nad z to the lne generator
284 series
.append("path")
285 .attr("class", "line")
286 .attr("d", function (d
) {
287 // console.log(d); // to see how d3 iterates through series
288 return valueline(d
.values
);
290 .style("stroke", function (d
) {
291 return color(d
.name
);
295 svg
.append("g") // Add the X Axis
296 .attr("class", "x axis")
297 .attr("transform", "translate(0," + height
+ ")")
300 .attr("transform", function (d
) {
301 return "rotate(-30)";
305 svg
.append("g") // Add the Y Axis
306 .attr("class", "y axis")
311 console
.log($scope
.graph_data
);
314 })(angular
, CRM
.$, CRM
._
);