gomarketme-react-native
Version:
Affiliate Marketing for React Native-Based iOS and Android Apps.
545 lines (544 loc) • 27.3 kB
JavaScript
"use strict";
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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["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 };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SaleDistribution = exports.Affiliate = exports.Campaign = exports.GoMarketMeAffiliateMarketingData = void 0;
var react_native_1 = require("react-native");
var react_native_device_info_1 = __importDefault(require("react-native-device-info"));
var get_user_locale_1 = __importDefault(require("get-user-locale"));
var async_storage_1 = __importDefault(require("@react-native-async-storage/async-storage"));
var axios_1 = __importDefault(require("axios"));
var react_native_iap_1 = require("react-native-iap");
var GoMarketMeAffiliateMarketingData = /** @class */ (function () {
function GoMarketMeAffiliateMarketingData(campaign, affiliate, saleDistribution, affiliateCampaignCode, deviceId, offerCode) {
this.campaign = campaign;
this.affiliate = affiliate;
this.saleDistribution = saleDistribution;
this.affiliateCampaignCode = affiliateCampaignCode;
this.deviceId = deviceId;
this.offerCode = offerCode;
}
GoMarketMeAffiliateMarketingData.fromJson = function (json) {
if (!json || Object.keys(json).length === 0)
return null;
return new GoMarketMeAffiliateMarketingData(Campaign.fromJson(json.campaign), Affiliate.fromJson(json.affiliate), SaleDistribution.fromJson(json.sale_distribution), json.affiliate_campaign_code || '', json.device_id || '', json.offer_code);
};
return GoMarketMeAffiliateMarketingData;
}());
exports.GoMarketMeAffiliateMarketingData = GoMarketMeAffiliateMarketingData;
var Campaign = /** @class */ (function () {
function Campaign(id, name, status, type, publicLinkUrl) {
this.id = id;
this.name = name;
this.status = status;
this.type = type;
this.publicLinkUrl = publicLinkUrl;
}
Campaign.fromJson = function (json) {
return new Campaign((json === null || json === void 0 ? void 0 : json.id) || '', (json === null || json === void 0 ? void 0 : json.name) || '', (json === null || json === void 0 ? void 0 : json.status) || '', (json === null || json === void 0 ? void 0 : json.type) || '', json === null || json === void 0 ? void 0 : json.public_link_url);
};
return Campaign;
}());
exports.Campaign = Campaign;
var Affiliate = /** @class */ (function () {
function Affiliate(id, firstName, lastName, countryCode, instagramAccount, tiktokAccount, xAccount) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.countryCode = countryCode;
this.instagramAccount = instagramAccount;
this.tiktokAccount = tiktokAccount;
this.xAccount = xAccount;
}
Affiliate.fromJson = function (json) {
return new Affiliate((json === null || json === void 0 ? void 0 : json.id) || '', (json === null || json === void 0 ? void 0 : json.first_name) || '', (json === null || json === void 0 ? void 0 : json.last_name) || '', (json === null || json === void 0 ? void 0 : json.country_code) || '', (json === null || json === void 0 ? void 0 : json.instagram_account) || '', (json === null || json === void 0 ? void 0 : json.tiktok_account) || '', (json === null || json === void 0 ? void 0 : json.x_account) || '');
};
return Affiliate;
}());
exports.Affiliate = Affiliate;
var SaleDistribution = /** @class */ (function () {
function SaleDistribution(platformPercentage, affiliatePercentage) {
this.platformPercentage = platformPercentage;
this.affiliatePercentage = affiliatePercentage;
}
SaleDistribution.fromJson = function (json) {
return new SaleDistribution((json === null || json === void 0 ? void 0 : json.platform_percentage) || '', (json === null || json === void 0 ? void 0 : json.affiliate_percentage) || '');
};
return SaleDistribution;
}());
exports.SaleDistribution = SaleDistribution;
var GoMarketMe = /** @class */ (function () {
function GoMarketMe() {
this.sdkType = 'ReactNative';
this.sdkVersion = '4.0.1';
this.sdkInitializedKey = 'GOMARKETME_SDK_INITIALIZED';
this.sdkInitializationUrl = 'https://4v9008q1a5.execute-api.us-west-2.amazonaws.com/prod/v1/sdk-initialization';
this.systemInfoUrl = 'https://4v9008q1a5.execute-api.us-west-2.amazonaws.com/prod/v1/mobile/system-info';
this.eventUrl = 'https://4v9008q1a5.execute-api.us-west-2.amazonaws.com/prod/v1/event';
this._affiliateCampaignCode = '';
this._deviceId = '';
this._packageName = '';
}
GoMarketMe.getInstance = function () {
if (!GoMarketMe.instance)
GoMarketMe.instance = new GoMarketMe();
return GoMarketMe.instance;
};
GoMarketMe.prototype.initialize = function (apiKey) {
return __awaiter(this, void 0, void 0, function () {
var isSDKInitialized, systemInfo, _a, e_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 7, , 8]);
return [4 /*yield*/, this._isSDKInitialized()];
case 1:
isSDKInitialized = _b.sent();
if (!!isSDKInitialized) return [3 /*break*/, 3];
return [4 /*yield*/, this._postSDKInitialization(apiKey)];
case 2:
_b.sent();
_b.label = 3;
case 3:
this._packageName = react_native_device_info_1.default.getBundleId();
return [4 /*yield*/, this._getSystemInfo()];
case 4:
systemInfo = _b.sent();
_a = this;
return [4 /*yield*/, this._postSystemInfo(systemInfo, apiKey)];
case 5:
_a.affiliateMarketingData = _b.sent();
return [4 /*yield*/, this._addListeners(apiKey)];
case 6:
_b.sent();
this._fetchExistingPurchases(apiKey).catch(function (e) {
console.log('Error in background purchase fetch:', e);
});
return [3 /*break*/, 8];
case 7:
e_1 = _b.sent();
console.log('Error initializing GoMarketMe:', e_1);
return [3 /*break*/, 8];
case 8: return [2 /*return*/];
}
});
});
};
GoMarketMe.prototype.removeListeners = function () {
var _a, _b, _c, _d;
try {
(_b = (_a = this._purchaseUpdateUnsub) === null || _a === void 0 ? void 0 : _a.remove) === null || _b === void 0 ? void 0 : _b.call(_a);
(_d = (_c = this._purchaseErrorUnsub) === null || _c === void 0 ? void 0 : _c.remove) === null || _d === void 0 ? void 0 : _d.call(_c);
}
catch (e) {
console.log('Error removing listeners:', e);
}
};
GoMarketMe.prototype._addListeners = function (apiKey) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
if (!this._purchaseUpdateUnsub) {
this._purchaseUpdateUnsub = (0, react_native_iap_1.purchaseUpdatedListener)(function (purchase) { return __awaiter(_this, void 0, void 0, function () {
var e_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this._fetchConsolidatedPurchases([purchase], apiKey)];
case 1:
_a.sent();
return [3 /*break*/, 3];
case 2:
e_2 = _a.sent();
console.log('purchaseUpdated error:', e_2);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); });
}
if (!this._purchaseErrorUnsub) {
this._purchaseErrorUnsub = (0, react_native_iap_1.purchaseErrorListener)(function (error) {
console.log('purchaseErrorListener:', error);
});
}
return [2 /*return*/];
});
});
};
GoMarketMe.prototype._getSystemInfo = function () {
return __awaiter(this, void 0, void 0, function () {
var deviceData, _a, devicePixelRatio, dimension;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!(react_native_1.Platform.OS === 'ios')) return [3 /*break*/, 2];
return [4 /*yield*/, this._readIosDeviceInfo()];
case 1:
_a = _c.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, this._readAndroidDeviceInfo()];
case 3:
_a = _c.sent();
_c.label = 4;
case 4:
deviceData = _a;
this._deviceId = deviceData['deviceId'] || '';
devicePixelRatio = react_native_1.PixelRatio.get();
dimension = react_native_1.Dimensions.get('window');
_b = {
device_info: deviceData,
window_info: {
devicePixelRatio: devicePixelRatio,
width: dimension.width * devicePixelRatio,
height: dimension.height * devicePixelRatio,
},
time_zone: Intl.DateTimeFormat().resolvedOptions().timeZone,
language_code: (0, get_user_locale_1.default)(),
sdk_type: this.sdkType,
sdk_version: this.sdkVersion,
package_name: this._packageName
};
return [4 /*yield*/, this._isProduction()];
case 5: return [2 /*return*/, (_b.is_production = _c.sent(),
_b)];
}
});
});
};
GoMarketMe.prototype._postSDKInitialization = function (apiKey) {
return __awaiter(this, void 0, void 0, function () {
var res, e_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 4, , 5]);
return [4 /*yield*/, axios_1.default.post(this.sdkInitializationUrl, {}, {
headers: { 'x-api-key': apiKey },
})];
case 1:
res = _a.sent();
if (!(res.status === 200)) return [3 /*break*/, 3];
return [4 /*yield*/, this._markSDKAsInitialized()];
case 2:
_a.sent();
_a.label = 3;
case 3: return [3 /*break*/, 5];
case 4:
e_3 = _a.sent();
console.log('Error posting SDK initialization:', e_3);
return [3 /*break*/, 5];
case 5: return [2 /*return*/];
}
});
});
};
GoMarketMe.prototype._postSystemInfo = function (data, apiKey) {
return __awaiter(this, void 0, void 0, function () {
var res, obj, e_4;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, axios_1.default.post(this.systemInfoUrl, data, {
headers: { 'x-api-key': apiKey },
})];
case 1:
res = _a.sent();
if (res.status === 200) {
obj = GoMarketMeAffiliateMarketingData.fromJson(res.data);
if (obj)
this._affiliateCampaignCode = obj.affiliateCampaignCode;
return [2 /*return*/, obj];
}
return [3 /*break*/, 3];
case 2:
e_4 = _a.sent();
console.log('Error posting system info:', e_4);
return [3 /*break*/, 3];
case 3: return [2 /*return*/, null];
}
});
});
};
GoMarketMe.prototype._readAndroidDeviceInfo = function () {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = {};
return [4 /*yield*/, react_native_device_info_1.default.getAndroidId()];
case 1:
_a.deviceId = _b.sent();
return [4 /*yield*/, react_native_device_info_1.default.getBrand()];
case 2:
_a.brand = _b.sent();
return [4 /*yield*/, react_native_device_info_1.default.getModel()];
case 3:
_a.model = _b.sent();
return [4 /*yield*/, react_native_device_info_1.default.getManufacturer()];
case 4:
_a.manufacturer = _b.sent();
return [4 /*yield*/, react_native_device_info_1.default.getSystemName()];
case 5:
_a.systemName = _b.sent();
return [4 /*yield*/, react_native_device_info_1.default.getSystemVersion()];
case 6: return [2 /*return*/, (_a.systemVersion = _b.sent(),
_a)];
}
});
});
};
GoMarketMe.prototype._readIosDeviceInfo = function () {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = {};
return [4 /*yield*/, react_native_device_info_1.default.getUniqueId()];
case 1:
_a.deviceId = _b.sent();
return [4 /*yield*/, react_native_device_info_1.default.getBrand()];
case 2:
_a.brand = _b.sent();
return [4 /*yield*/, react_native_device_info_1.default.getModel()];
case 3:
_a.model = _b.sent();
return [4 /*yield*/, react_native_device_info_1.default.getManufacturer()];
case 4:
_a.manufacturer = _b.sent();
return [4 /*yield*/, react_native_device_info_1.default.getSystemName()];
case 5:
_a.systemName = _b.sent();
return [4 /*yield*/, react_native_device_info_1.default.getSystemVersion()];
case 6: return [2 /*return*/, (_a.systemVersion = _b.sent(),
_a)];
}
});
});
};
GoMarketMe.prototype._fetchConsolidatedPurchases = function (purchases, apiKey) {
return __awaiter(this, void 0, void 0, function () {
var _i, purchases_1, purchase, receipt, data, pid, products, _a, products_1, p, e_5;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_i = 0, purchases_1 = purchases;
_b.label = 1;
case 1:
if (!(_i < purchases_1.length)) return [3 /*break*/, 8];
purchase = purchases_1[_i];
receipt = purchase.purchaseToken;
if (!receipt)
return [3 /*break*/, 7];
data = this._serializePurchaseDetails(purchase);
data.products = [];
pid = data.productID || '';
if (!pid) return [3 /*break*/, 5];
_b.label = 2;
case 2:
_b.trys.push([2, 4, , 5]);
return [4 /*yield*/, (0, react_native_iap_1.fetchProducts)({ skus: [pid] })];
case 3:
products = _b.sent();
if (products && products.length) {
for (_a = 0, products_1 = products; _a < products_1.length; _a++) {
p = products_1[_a];
data.products.push(this._serializeProductDetails(p));
}
}
return [3 /*break*/, 5];
case 4:
e_5 = _b.sent();
console.warn('Error fetching products:', e_5);
return [3 /*break*/, 5];
case 5: return [4 /*yield*/, this._sendEventToServer(JSON.stringify(data), 'purchase_product', apiKey)];
case 6:
_b.sent();
_b.label = 7;
case 7:
_i++;
return [3 /*break*/, 1];
case 8: return [2 /*return*/];
}
});
});
};
GoMarketMe.prototype._sendEventToServer = function (body, eventType, apiKey) {
return __awaiter(this, void 0, void 0, function () {
var e_6;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, axios_1.default.post(this.eventUrl, body, {
headers: {
'x-affiliate-campaign-code': this._affiliateCampaignCode,
'x-device-id': this._deviceId,
'x-event-type': eventType,
'x-product-type': react_native_1.Platform.OS,
'x-source-name': react_native_1.Platform.OS === 'android' ? 'google_play' : 'app_store',
'x-api-key': apiKey,
},
})];
case 1:
_a.sent();
console.log("".concat(eventType, " sent successfully"));
return [3 /*break*/, 3];
case 2:
e_6 = _a.sent();
console.log("Error sending ".concat(eventType, ":"), e_6);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
});
};
GoMarketMe.prototype._serializePurchaseDetails = function (purchase) {
return {
packageName: this._packageName,
productID: purchase.productId,
purchaseID: purchase.id,
transactionDate: purchase.transactionDate,
verificationData: {
localVerificationData: purchase.purchaseToken,
source: react_native_1.Platform.OS === 'android' ? 'google_play' : 'app_store',
},
};
};
GoMarketMe.prototype._serializeProductDetails = function (product) {
return {
packageName: this._packageName,
productID: product.id,
productTitle: product.title,
productDescription: product.description,
productPrice: product.displayPrice,
productRawPrice: product.price || '',
productCurrencyCode: product.currency,
};
};
GoMarketMe.prototype._markSDKAsInitialized = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, async_storage_1.default.setItem(this.sdkInitializedKey, 'true')];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
GoMarketMe.prototype._isSDKInitialized = function () {
return __awaiter(this, void 0, void 0, function () {
var val;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, async_storage_1.default.getItem(this.sdkInitializedKey)];
case 1:
val = _a.sent();
return [2 /*return*/, val === 'true'];
}
});
});
};
GoMarketMe.prototype._isProduction = function () {
return __awaiter(this, void 0, void 0, function () {
var installer, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (__DEV__)
return [2 /*return*/, false];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, react_native_device_info_1.default.getInstallerPackageName()];
case 2:
installer = _a.sent();
if (installer === 'com.android.vending' ||
installer === 'com.amazon.venezia') {
return [2 /*return*/, true];
}
if (installer == null && !__DEV__) {
return [2 /*return*/, true];
}
return [2 /*return*/, false];
case 3:
error_1 = _a.sent();
console.warn('Error determining production status:', error_1);
return [2 /*return*/, false];
case 4: return [2 /*return*/];
}
});
});
};
GoMarketMe.prototype._fetchExistingPurchases = function (apiKey) {
return __awaiter(this, void 0, void 0, function () {
var purchases, e_7;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 4, , 5]);
return [4 /*yield*/, (0, react_native_iap_1.getAvailablePurchases)()];
case 1:
purchases = _a.sent();
if (!(purchases && purchases.length > 0)) return [3 /*break*/, 3];
return [4 /*yield*/, this._fetchConsolidatedPurchases(purchases, apiKey)];
case 2:
_a.sent();
_a.label = 3;
case 3: return [3 /*break*/, 5];
case 4:
e_7 = _a.sent();
console.log('Error fetching existing purchases on app open:', e_7);
return [3 /*break*/, 5];
case 5: return [2 /*return*/];
}
});
});
};
return GoMarketMe;
}());
exports.default = GoMarketMe.getInstance();