mixpanel.js
1.59 KB
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
/* global wp_smush_mixpanel */
import mixpanel from "mixpanel-browser";
export default class MixPanel {
constructor() {
this.mixpanelInstance = mixpanel.init(wp_smush_mixpanel.token, {
opt_out_tracking_by_default: !wp_smush_mixpanel.opt_in,
loaded: (mixpanel) => {
mixpanel.identify(wp_smush_mixpanel.unique_id);
mixpanel.register(wp_smush_mixpanel.super_properties);
const trackingActiveInSmushSettings = !!wp_smush_mixpanel.opt_in;
if (mixpanel.has_opted_in_tracking() !== trackingActiveInSmushSettings) {
// The status cached by MixPanel in the local storage is different from the settings. Clear the cache.
mixpanel.clear_opt_in_out_tracking();
}
}
}, 'smush');
}
track(event, properties = {}) {
this.mixpanelInstance.track(event, properties);
}
trackBulkSmushCompleted( globalStats ) {
const {
savings_bytes,
count_images,
percent_optimized,
savings_percent,
count_resize,
savings_resize
} = globalStats;
this.track('Bulk Smush Completed', {
'Total Savings': this.convertToMegabytes( savings_bytes ),
'Total Images': count_images,
'Media Optimization Percentage': parseFloat( percent_optimized ),
'Percentage of Savings': parseFloat( savings_percent ),
'Images Resized': count_resize,
'Resize Savings': this.convertToMegabytes( savings_resize )
});
}
trackBulkSmushCancel() {
this.track('Bulk Smush Cancelled');
}
convertToMegabytes( sizeInBytes ) {
const unitMB = Math.pow( 1024, 2 );
const sizeInMegabytes = sizeInBytes/unitMB;
return sizeInMegabytes && parseFloat(sizeInMegabytes.toFixed(2)) || 0;
}
}