UNPKG

react-native-kochava-measurement

Version:

A lightweight and easy to integrate SDK, providing first-class integration with Kochava’s installation attribution and analytics platform.

283 lines (282 loc) 14.9 kB
/* Authored by Brett Barinaga on 11/17/21. Copyright (c) Kochava, Inc. All rights reserved. */ var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import * as http from "../http"; import * as utils from "../utils/utils"; import * as browser from "../browser/browser"; import { Log } from "../utils/log"; import { constructPayload } from "./payload"; import { getPersistedIdentityLinks, PersistKey, readAndUpdatePersistedValue, updateInstallCount, updateLastInstall, updatePersistedValue } from "../browser/persist"; var timeOut; var canceled = false; export var build = function (instance) { return __awaiter(void 0, void 0, void 0, function () { var payload, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: payload = constructPayload("install", instance); instance.installStarted = true; _a = payload; return [4 /*yield*/, constructInstallData(instance)]; case 1: _a.data = _b.sent(); Log.debug("Install Payload", JSON.stringify(payload)); if (instance.kochavaConfig.privacy) { instance.kochavaConfig.privacy.deny_datapoints.forEach(function (denyPoint) { for (var point in payload.data) { var key = point; if (key === denyPoint) delete payload.data[key]; } for (var point in payload) { var key = point; if (key === denyPoint) delete payload[key]; } }); } return [2 /*return*/, payload]; } }); }); }; export var cancelRetries = function () { canceled = true; clearTimeout(timeOut); }; export var send = function (instance, payload, retries) { if (retries === void 0) { retries = 0; } return __awaiter(void 0, void 0, void 0, function () { var success, resp, _loop_1, state_1; return __generator(this, function (_a) { switch (_a.label) { case 0: canceled = false; success = false; _loop_1 = function () { var sendTime, respStr, respTime, lastUpdateObject, retryWaterfall, retryIndex, retrySec_1; return __generator(this, function (_b) { switch (_b.label) { case 0: sendTime = utils.getCurrTimeMS() - instance.startTimeMS; Log.diagDebug("Sending install at ".concat(utils.formatTime(sendTime / 1000), " seconds")); respStr = void 0; if (!!instance.sdkDisabled) return [3 /*break*/, 2]; return [4 /*yield*/, http.sendRequest(payload, (instance.overrideUrls.install) ? instance.overrideUrls.install : instance.kochavaConfig.networking.urls.install)]; case 1: respStr = _b.sent(); return [3 /*break*/, 3]; case 2: respStr = http.mockSuccess(); _b.label = 3; case 3: respTime = utils.getCurrTimeMS() - instance.startTimeMS; Log.diagDebug("Completed install at ".concat(new Date().toLocaleTimeString(), " \n seconds with a network duration of ").concat(utils.formatTime((respTime - sendTime) / 1000), " seconds")); if (canceled) { Log.trace("Can no longer retry install, cancelling."); return [2 /*return*/, { value: false }]; } try { Log.trace("Install Response string:", respStr); resp = JSON.parse(respStr); Log.debug("Install Response:", respStr); success = http.wasRespSuccess(resp.success); } catch (e) { Log.error("Error parsing Install Response", e); success = false; } if (!success) return [3 /*break*/, 6]; Log.info("Install success!"); instance.installCount++; return [4 /*yield*/, updateInstallCount(instance.installCount, instance.useCookies)]; case 4: _b.sent(); lastUpdateObject = constructLastInstall(payload, sendTime, instance); return [4 /*yield*/, updateLastInstall(lastUpdateObject, instance.useCookies)]; case 5: _b.sent(); return [2 /*return*/, { value: success }]; case 6: if (!!canceled) return [3 /*break*/, 8]; retryWaterfall = instance.kochavaConfig.networking.retry_waterfall; retryIndex = (retries > retryWaterfall.length - 1) ? retryWaterfall.length - 1 : retries; retrySec_1 = retryWaterfall[retryIndex]; Log.error("Install failed, attempting again in ".concat(retrySec_1, " seconds")); return [4 /*yield*/, new Promise(function (resolve) { timeOut = setTimeout(resolve, retrySec_1 * 1000); })]; case 7: _b.sent(); retries++; _b.label = 8; case 8: return [2 /*return*/]; } }); }; _a.label = 1; case 1: return [5 /*yield**/, _loop_1()]; case 2: state_1 = _a.sent(); if (typeof state_1 === "object") return [2 /*return*/, state_1.value]; _a.label = 3; case 3: if (!success && !canceled) return [3 /*break*/, 1]; _a.label = 4; case 4: return [2 /*return*/]; } }); }); }; export var onSuccess = function (instance) { instance.kochavaInstallDate = utils.getCurrTimeMS(); updatePersistedValue(PersistKey.InstallSentDate, String(instance.kochavaInstallDate), instance.useCookies); }; var constructLastInstall = function (payload, sendTime, instance) { var lastInstall = { kochava_device_id: payload.kochava_device_id, kochava_app_id: payload.kochava_app_id, sdk_version: payload.sdk_version, time: sendTime, count: instance.installCount }; if (instance.sdkDisabled) { lastInstall['sdk_disabled'] = true; } return lastInstall; }; var constructInstallData = function (instance) { return __awaiter(void 0, void 0, void 0, function () { var currTime, uptime, returnObj, persistedIdLinks, key, includeKey, _i, _a, denyIdLinkKey; var _b; return __generator(this, function (_c) { switch (_c.label) { case 0: currTime = Math.floor(Date.now() / 1000); uptime = (currTime - instance.startTimeMS) / 1000; if (uptime < 0.0) uptime = 0.0; _b = { starttime: instance.startTimeMS / 1000, uptime: uptime }; return [4 /*yield*/, determineInstallUserTime(instance, currTime)]; case 1: returnObj = (_b.usertime = _c.sent(), _b.device_orientation = browser.getDeviceOrientation(), _b.package = browser.getPackageName(), _b.disp_w = browser.getDeviceWidth(), _b.disp_h = browser.getDeviceHeight(), _b.language = browser.getLanguage(), _b); if (instance.utm) { returnObj = __assign(__assign({}, returnObj), { conversion_data: { utm_source: instance.utm } }); } if (instance.customValues.length > 0) returnObj.device_ids = {}; instance.customValues.forEach(function (custom) { var customKey = Object.keys(custom.data)[0]; if (instance.kochavaConfig.privacy) { var keyAllowed = false; for (var _i = 0, _a = instance.kochavaConfig.privacy.allow_custom_ids; _i < _a.length; _i++) { var allowed = _a[_i]; if (customKey === allowed) { keyAllowed = true; } } if (keyAllowed) { if (custom.isDeviceId) returnObj.device_ids = __assign(__assign({}, returnObj.device_ids), custom.data); else returnObj = __assign(__assign({}, custom.data), returnObj); } } }); return [4 /*yield*/, getPersistedIdentityLinks()]; case 2: persistedIdLinks = _c.sent(); if (persistedIdLinks) { returnObj.identity_link = {}; for (key in persistedIdLinks) { includeKey = true; for (_i = 0, _a = instance.kochavaConfig.privacy.deny_identity_links; _i < _a.length; _i++) { denyIdLinkKey = _a[_i]; if (denyIdLinkKey === key) { Log.debug("Denied identity_link ".concat(denyIdLinkKey, ", dropping from install.")); includeKey = false; } } if (includeKey) returnObj.identity_link[key] = persistedIdLinks[key]; } } return [2 /*return*/, returnObj]; } }); }); }; var determineInstallUserTime = function (instance, currTime) { return __awaiter(void 0, void 0, void 0, function () { var firstStartStr, firstStart; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, readAndUpdatePersistedValue(PersistKey.FirstStartDate, instance.useCookies)]; case 1: firstStartStr = _a.sent(); if (firstStartStr) { firstStart = JSON.parse(firstStartStr); // If its been 30 days since the sdk started if ((currTime - firstStart) > 2592000) { return [2 /*return*/, currTime]; } return [2 /*return*/, firstStart]; } return [2 /*return*/, currTime]; } }); }); };