UNPKG

6.21 kBJavaScriptView Raw
1import { parseSemanticVersion } from './util';
2/**
3 * @hidden
4 */
5var Stat = (function () {
6 function Stat(appId, stat, value) {
7 if (value === void 0) { value = 1; }
8 this.appId = appId;
9 this.stat = stat;
10 this.value = value;
11 this.appId = appId;
12 this.stat = stat;
13 this.value = value;
14 this.created = new Date();
15 }
16 Stat.prototype.toJSON = function () {
17 return {
18 app_id: this.appId,
19 stat: this.stat,
20 value: this.value,
21 created: this.created.toISOString(),
22 };
23 };
24 return Stat;
25}());
26export { Stat };
27/**
28 * A client for Insights that handles batching, user activity insight, and
29 * sending insights at an interval.
30 *
31 * @featured
32 */
33var Insights = (function () {
34 function Insights(deps, options) {
35 if (options === void 0) { options = {}; }
36 var _this = this;
37 this.options = options;
38 this.app = deps.appStatus;
39 this.storage = deps.storage;
40 this.config = deps.config;
41 this.client = deps.client;
42 this.device = deps.device;
43 this.logger = deps.logger;
44 this.batch = [];
45 if (typeof this.options.enabled === 'undefined') {
46 this.options.enabled = true;
47 }
48 if (typeof this.options.intervalSubmit === 'undefined') {
49 this.options.intervalSubmit = 60 * 1000;
50 }
51 if (typeof this.options.intervalActiveCheck === 'undefined') {
52 this.options.intervalActiveCheck = 1000;
53 }
54 if (typeof this.options.submitCount === 'undefined') {
55 this.options.submitCount = 100;
56 }
57 if (this.options.enabled) {
58 if (this.options.intervalSubmit) {
59 setInterval(function () {
60 _this.submit();
61 }, this.options.intervalSubmit);
62 }
63 if (this.options.intervalActiveCheck) {
64 setInterval(function () {
65 if (!_this.app.closed) {
66 _this.checkActivity();
67 }
68 }, this.options.intervalActiveCheck);
69 }
70 }
71 }
72 /**
73 * Track an insight.
74 *
75 * Insights are put into a submission queue for batching to save network
76 * usage. This means that insights aren't sent immediately when this method
77 * is called.
78 *
79 * @param stat - The insight name. Insights tracked by the Cloud Client must
80 * have the prefix `mobileapp`. For example, an insight would track the
81 * number of login button clicks could be named
82 * `mobileapp.login_button.clicks`.
83 * @param value - The number by which to increment this insight (defaults to 1).
84 */
85 Insights.prototype.track = function (stat, value) {
86 if (value === void 0) { value = 1; }
87 if (this.options.enabled) {
88 this.trackStat(new Stat(this.config.get('app_id'), stat, value));
89 }
90 else {
91 this.logger.warn('Ionic Insights: Will not track(), insights are not enabled.');
92 }
93 };
94 /**
95 * @private
96 */
97 Insights.prototype.checkActivity = function () {
98 var session = this.storage.get('insights_session');
99 if (!session) {
100 this.markActive();
101 }
102 else {
103 var d = new Date(session);
104 var hour = 60 * 60 * 1000;
105 if (d.getTime() + hour < new Date().getTime()) {
106 this.markActive();
107 }
108 }
109 };
110 /**
111 * @private
112 */
113 Insights.prototype.markActive = function () {
114 this.track('mobileapp.active');
115 if (!this.device.native || typeof this.device.native.platform !== 'string') {
116 this.logger.warn('Ionic Insights: Device information unavailable.');
117 }
118 else {
119 var device = this.device.native;
120 var platform = this.normalizeDevicePlatform(device.platform);
121 var platformVersion = this.normalizeVersion(device.version);
122 var cordovaVersion = this.normalizeVersion(device.cordova);
123 this.track("mobileapp.active.platform." + platform);
124 this.track("mobileapp.active.platform." + platform + "." + platformVersion);
125 this.track("mobileapp.active.cordova." + cordovaVersion);
126 }
127 this.storage.set('insights_session', new Date().toISOString());
128 };
129 /**
130 * @private
131 */
132 Insights.prototype.normalizeDevicePlatform = function (platform) {
133 return platform.toLowerCase().replace(/[^a-z0-9_]/g, '_');
134 };
135 /**
136 * @private
137 */
138 Insights.prototype.normalizeVersion = function (s) {
139 var v;
140 try {
141 v = String(parseSemanticVersion(s).major);
142 }
143 catch (e) {
144 v = 'unknown';
145 }
146 return v;
147 };
148 /**
149 * @private
150 */
151 Insights.prototype.trackStat = function (stat) {
152 this.batch.push(stat);
153 if (this.shouldSubmit()) {
154 this.submit();
155 }
156 };
157 /**
158 * @private
159 */
160 Insights.prototype.shouldSubmit = function () {
161 return this.batch.length >= this.options.submitCount;
162 };
163 /**
164 * Manually submit the insights that have been tracked and stored in the
165 * submission queue.
166 *
167 * Unless configured differently, insights are automatically sent to Ionic
168 * every minute, by default. It may be prudent, however, to call this when
169 * you know your app is about to be put to sleep.
170 */
171 Insights.prototype.submit = function () {
172 var _this = this;
173 if (this.batch.length === 0) {
174 return;
175 }
176 var insights = [];
177 for (var _i = 0, _a = this.batch; _i < _a.length; _i++) {
178 var stat = _a[_i];
179 insights.push(stat.toJSON());
180 }
181 this.client.post('/insights')
182 .send({ 'insights': insights })
183 .end(function (err, res) {
184 if (err) {
185 _this.logger.error('Ionic Insights: Could not send insights.', err);
186 }
187 });
188 this.batch = [];
189 };
190 return Insights;
191}());
192export { Insights };