UNPKG

4.98 kBJavaScriptView Raw
1"use strict";
2var util_1 = require('./util');
3/**
4 * @hidden
5 */
6var Stat = (function () {
7 function Stat(appId, stat, value) {
8 if (value === void 0) { value = 1; }
9 this.appId = appId;
10 this.stat = stat;
11 this.value = value;
12 this.appId = appId;
13 this.stat = stat;
14 this.value = value;
15 this.created = new Date();
16 }
17 Stat.prototype.toJSON = function () {
18 return {
19 app_id: this.appId,
20 stat: this.stat,
21 value: this.value,
22 created: this.created.toISOString(),
23 };
24 };
25 return Stat;
26}());
27exports.Stat = Stat;
28/**
29 * A client for Insights that handles batching, user activity insight, and
30 * sending insights at an interval.
31 *
32 * @hidden
33 */
34var Insights = (function () {
35 function Insights(deps, options) {
36 var _this = this;
37 if (options === void 0) { options = {}; }
38 this.options = options;
39 this.app = deps.appStatus;
40 this.storage = deps.storage;
41 this.config = deps.config;
42 this.client = deps.client;
43 this.device = deps.device;
44 this.logger = deps.logger;
45 this.batch = [];
46 if (typeof this.options.intervalSubmit === 'undefined') {
47 this.options.intervalSubmit = 60 * 1000;
48 }
49 if (typeof this.options.intervalActiveCheck === 'undefined') {
50 this.options.intervalActiveCheck = 1000;
51 }
52 if (typeof this.options.submitCount === 'undefined') {
53 this.options.submitCount = 100;
54 }
55 if (this.options.intervalSubmit) {
56 setInterval(function () {
57 _this.submit();
58 }, this.options.intervalSubmit);
59 }
60 if (this.options.intervalActiveCheck) {
61 setInterval(function () {
62 if (!_this.app.closed) {
63 _this.checkActivity();
64 }
65 }, this.options.intervalActiveCheck);
66 }
67 }
68 /**
69 * Track an insight.
70 *
71 * @param stat - The insight name.
72 * @param value - The number by which to increment this insight.
73 */
74 Insights.prototype.track = function (stat, value) {
75 if (value === void 0) { value = 1; }
76 this.trackStat(new Stat(this.config.settings.core.app_id, stat, value));
77 };
78 Insights.prototype.checkActivity = function () {
79 var session = this.storage.get('insights_session');
80 if (!session) {
81 this.markActive();
82 }
83 else {
84 var d = new Date(session);
85 var hour = 60 * 60 * 1000;
86 if (d.getTime() + hour < new Date().getTime()) {
87 this.markActive();
88 }
89 }
90 };
91 Insights.prototype.markActive = function () {
92 this.track('mobileapp.active');
93 if (!this.device.native || !this.device.native.device || !this.device.native.device.platform) {
94 this.logger.warn('Ionic Insights: Device information unavailable.');
95 }
96 else {
97 var device = this.device.native.device;
98 var platform = this.normalizeDevicePlatform(device.platform);
99 var platformVersion = this.normalizeVersion(device.version);
100 var cordovaVersion = this.normalizeVersion(device.cordova);
101 this.track("mobileapp.active.platform." + platform);
102 this.track("mobileapp.active.platform." + platform + "." + platformVersion);
103 this.track("mobileapp.active.cordova." + cordovaVersion);
104 }
105 this.storage.set('insights_session', new Date().toISOString());
106 };
107 Insights.prototype.normalizeDevicePlatform = function (platform) {
108 return platform.toLowerCase().replace(/[^a-z0-9_]/g, '_');
109 };
110 Insights.prototype.normalizeVersion = function (s) {
111 var v;
112 try {
113 v = String(util_1.parseSemanticVersion(s).major);
114 }
115 catch (e) {
116 v = 'unknown';
117 }
118 return v;
119 };
120 Insights.prototype.trackStat = function (stat) {
121 this.batch.push(stat);
122 if (this.shouldSubmit()) {
123 this.submit();
124 }
125 };
126 Insights.prototype.shouldSubmit = function () {
127 return this.batch.length >= this.options.submitCount;
128 };
129 Insights.prototype.submit = function () {
130 var _this = this;
131 if (this.batch.length === 0) {
132 return;
133 }
134 var insights = [];
135 for (var _i = 0, _a = this.batch; _i < _a.length; _i++) {
136 var stat = _a[_i];
137 insights.push(stat.toJSON());
138 }
139 this.client.post('/insights')
140 .send({ 'insights': insights })
141 .end(function (err, res) {
142 if (err) {
143 _this.logger.error('Ionic Insights: Could not send insights.', err);
144 }
145 });
146 this.batch = [];
147 };
148 return Insights;
149}());
150exports.Insights = Insights;
151//# sourceMappingURL=insights.js.map
\No newline at end of file