@layers/react-native 0.1.0-alpha.0 → 0.2.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/chunk-DQxbwPpZ.js +30 -0
- package/dist/index.d.ts +118 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +743 -13
- package/dist/index.js.map +1 -1
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +1906 -928
- package/dist/plugin.js.map +1 -1
- package/ios/LayersSKAN.m +45 -0
- package/ios/LayersSKAN.swift +188 -0
- package/ios/LayersSKANCompat.h +23 -0
- package/ios/LayersSKANCompat.m +118 -0
- package/layers-react-native.podspec +25 -0
- package/package.json +17 -11
package/dist/index.js
CHANGED
|
@@ -1,8 +1,645 @@
|
|
|
1
|
+
import { n as __require } from "./chunk-DQxbwPpZ.js";
|
|
1
2
|
import { LayersClient } from "@layers/client";
|
|
2
|
-
import { Platform } from "react-native";
|
|
3
|
+
import { NativeModules, Platform } from "react-native";
|
|
4
|
+
import { useEffect } from "react";
|
|
3
5
|
|
|
4
6
|
export * from "@layers/client"
|
|
5
7
|
|
|
8
|
+
//#region src/native/SKANModule.ts
|
|
9
|
+
/**
|
|
10
|
+
* Mock SKAN implementation
|
|
11
|
+
*/
|
|
12
|
+
var MockSKANModule = class {
|
|
13
|
+
conversionValue = 0;
|
|
14
|
+
isRegistered = false;
|
|
15
|
+
attributionData = null;
|
|
16
|
+
async isAvailable() {
|
|
17
|
+
return Platform.OS === "ios";
|
|
18
|
+
}
|
|
19
|
+
async getVersion() {
|
|
20
|
+
return "4.0";
|
|
21
|
+
}
|
|
22
|
+
async registerAppForAdNetworkAttribution() {
|
|
23
|
+
this.isRegistered = true;
|
|
24
|
+
this.attributionData = {
|
|
25
|
+
adNetworkId: "mock.network.com",
|
|
26
|
+
campaignId: "12345",
|
|
27
|
+
sourceAppId: "123456789",
|
|
28
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
29
|
+
};
|
|
30
|
+
console.log("[Mock SKAN] App registered for attribution");
|
|
31
|
+
}
|
|
32
|
+
async updateConversionValue(value) {
|
|
33
|
+
if (value >= 0 && value <= 63) {
|
|
34
|
+
this.conversionValue = Math.max(this.conversionValue, value);
|
|
35
|
+
console.log(`[Mock SKAN] Conversion value updated to: ${value}`);
|
|
36
|
+
} else console.warn(`[Mock SKAN] Invalid conversion value: ${value}. Must be 0-63.`);
|
|
37
|
+
}
|
|
38
|
+
async updatePostbackConversionValue(value, coarseValue, lockWindow) {
|
|
39
|
+
this.conversionValue = Math.max(this.conversionValue, value);
|
|
40
|
+
console.log(`[Mock SKAN] Postback value updated: ${value}, coarse: ${coarseValue}, lock: ${lockWindow}`);
|
|
41
|
+
}
|
|
42
|
+
async startImpression(signature) {
|
|
43
|
+
console.log(`[Mock SKAN] Started impression: ${signature}`);
|
|
44
|
+
}
|
|
45
|
+
async endImpression(signature) {
|
|
46
|
+
console.log(`[Mock SKAN] Ended impression: ${signature}`);
|
|
47
|
+
}
|
|
48
|
+
async getCurrentConversionValue() {
|
|
49
|
+
return this.conversionValue;
|
|
50
|
+
}
|
|
51
|
+
async getAttributionData() {
|
|
52
|
+
return this.attributionData;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Get SKAN module instance
|
|
57
|
+
*/
|
|
58
|
+
function getSKANModule() {
|
|
59
|
+
if (Platform.OS === "ios") {
|
|
60
|
+
const nativeModule = NativeModules.LayersSKAN;
|
|
61
|
+
if (nativeModule) return nativeModule;
|
|
62
|
+
}
|
|
63
|
+
return new MockSKANModule();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* SKAN Manager - High-level wrapper for SKAdNetwork functionality with rule engine
|
|
67
|
+
*/
|
|
68
|
+
var SKANManager = class {
|
|
69
|
+
module;
|
|
70
|
+
onConversionUpdate;
|
|
71
|
+
currentPreset;
|
|
72
|
+
rules = [];
|
|
73
|
+
ruleEvaluationHistory = [];
|
|
74
|
+
constructor(onConversionUpdate) {
|
|
75
|
+
this.module = getSKANModule();
|
|
76
|
+
if (onConversionUpdate) this.onConversionUpdate = onConversionUpdate;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Initialize SKAN
|
|
80
|
+
*/
|
|
81
|
+
async initialize() {
|
|
82
|
+
if (await this.module.isAvailable()) await this.module.registerAppForAdNetworkAttribution();
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Check if SKAN is supported
|
|
86
|
+
*/
|
|
87
|
+
async isSupported() {
|
|
88
|
+
return this.module.isAvailable();
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get SKAN version
|
|
92
|
+
*/
|
|
93
|
+
async getVersion() {
|
|
94
|
+
return this.module.getVersion();
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Set conversion rules preset
|
|
98
|
+
*/
|
|
99
|
+
setPreset(preset) {
|
|
100
|
+
this.rules = this.getPresetConfig(preset).rules.sort((a, b) => (b.priority || 0) - (a.priority || 0));
|
|
101
|
+
this.currentPreset = preset;
|
|
102
|
+
console.log(`SKAN preset set to: ${preset} (${this.rules.length} rules)`);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Set custom conversion rules
|
|
106
|
+
*/
|
|
107
|
+
setCustomRules(rules) {
|
|
108
|
+
this.rules = rules.sort((a, b) => (b.priority || 0) - (a.priority || 0));
|
|
109
|
+
this.currentPreset = "custom";
|
|
110
|
+
console.log(`SKAN custom rules set: ${rules.length} rules`);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Process an event against conversion rules
|
|
114
|
+
*/
|
|
115
|
+
async processEvent(eventName, properties) {
|
|
116
|
+
if (!await this.isSupported()) return;
|
|
117
|
+
const ruleEvaluations = [];
|
|
118
|
+
let matchedRule;
|
|
119
|
+
for (const rule of this.rules) {
|
|
120
|
+
const evaluation = this.evaluateRule(rule, eventName, properties);
|
|
121
|
+
const ruleEvaluation = {
|
|
122
|
+
rule: rule.description || `${rule.eventName} (${rule.conversionValue})`,
|
|
123
|
+
matched: evaluation.matched
|
|
124
|
+
};
|
|
125
|
+
if (evaluation.reason) ruleEvaluation.reason = evaluation.reason;
|
|
126
|
+
ruleEvaluations.push(ruleEvaluation);
|
|
127
|
+
if (evaluation.matched && !matchedRule) {
|
|
128
|
+
matchedRule = rule;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const historyEntry = {
|
|
133
|
+
event: eventName,
|
|
134
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
135
|
+
};
|
|
136
|
+
if (matchedRule?.description || matchedRule?.eventName) historyEntry.rule = matchedRule.description || matchedRule.eventName;
|
|
137
|
+
this.ruleEvaluationHistory.push(historyEntry);
|
|
138
|
+
if (this.ruleEvaluationHistory.length > 100) this.ruleEvaluationHistory = this.ruleEvaluationHistory.slice(-100);
|
|
139
|
+
if (matchedRule) try {
|
|
140
|
+
const previousValue = await this.module.getCurrentConversionValue() || 0;
|
|
141
|
+
if (matchedRule.conversionValue > previousValue) {
|
|
142
|
+
if (await this.getVersion() === "4.0" && matchedRule.coarseValue) await this.module.updatePostbackConversionValue(matchedRule.conversionValue, matchedRule.coarseValue, matchedRule.lockWindow);
|
|
143
|
+
else await this.module.updateConversionValue(matchedRule.conversionValue);
|
|
144
|
+
if (this.onConversionUpdate) this.onConversionUpdate({
|
|
145
|
+
event: eventName,
|
|
146
|
+
properties,
|
|
147
|
+
matchedRule,
|
|
148
|
+
previousValue,
|
|
149
|
+
newValue: matchedRule.conversionValue,
|
|
150
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
151
|
+
ruleEvaluations
|
|
152
|
+
});
|
|
153
|
+
console.log(`SKAN: Event ${eventName} matched rule '${matchedRule.description || matchedRule.eventName}', value: ${previousValue} → ${matchedRule.conversionValue}`);
|
|
154
|
+
} else console.log(`SKAN: Event ${eventName} matched rule but value ${matchedRule.conversionValue} not higher than current ${previousValue}`);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
console.error("SKAN conversion value update failed:", error);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Evaluate if a rule matches an event
|
|
161
|
+
*/
|
|
162
|
+
evaluateRule(rule, eventName, properties) {
|
|
163
|
+
if (rule.eventName !== eventName) return {
|
|
164
|
+
matched: false,
|
|
165
|
+
reason: `Event name '${eventName}' != '${rule.eventName}'`
|
|
166
|
+
};
|
|
167
|
+
if (rule.conditions) for (const [key, expectedValue] of Object.entries(rule.conditions)) {
|
|
168
|
+
const actualValue = properties[key];
|
|
169
|
+
if (typeof expectedValue === "object" && expectedValue !== null) {
|
|
170
|
+
for (const [operator, comparisonValue] of Object.entries(expectedValue)) if (!this.evaluateCondition(actualValue, operator, comparisonValue).matched) return {
|
|
171
|
+
matched: false,
|
|
172
|
+
reason: `Condition ${key} ${operator} ${comparisonValue} failed (actual: ${actualValue})`
|
|
173
|
+
};
|
|
174
|
+
} else if (actualValue !== expectedValue) return {
|
|
175
|
+
matched: false,
|
|
176
|
+
reason: `Property ${key}: expected ${expectedValue}, got ${actualValue}`
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
return { matched: true };
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Evaluate a single condition with operator
|
|
183
|
+
*/
|
|
184
|
+
evaluateCondition(actualValue, operator, expectedValue) {
|
|
185
|
+
const numericActual = Number(actualValue);
|
|
186
|
+
const numericExpected = Number(expectedValue);
|
|
187
|
+
switch (operator) {
|
|
188
|
+
case ">": return { matched: numericActual > numericExpected };
|
|
189
|
+
case ">=": return { matched: numericActual >= numericExpected };
|
|
190
|
+
case "<": return { matched: numericActual < numericExpected };
|
|
191
|
+
case "<=": return { matched: numericActual <= numericExpected };
|
|
192
|
+
case "==":
|
|
193
|
+
case "=": return { matched: actualValue === expectedValue };
|
|
194
|
+
case "!=": return { matched: actualValue !== expectedValue };
|
|
195
|
+
default:
|
|
196
|
+
console.warn(`Unknown condition operator: ${operator}`);
|
|
197
|
+
return { matched: false };
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Get current conversion value
|
|
202
|
+
*/
|
|
203
|
+
async getCurrentValue() {
|
|
204
|
+
return this.module.getCurrentConversionValue();
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Get attribution data
|
|
208
|
+
*/
|
|
209
|
+
async getAttributionData() {
|
|
210
|
+
return this.module.getAttributionData();
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Get preset configuration
|
|
214
|
+
*/
|
|
215
|
+
getPresetConfig(preset) {
|
|
216
|
+
switch (preset) {
|
|
217
|
+
case "subscriptions": return {
|
|
218
|
+
name: "subscriptions",
|
|
219
|
+
description: "Optimized for subscription apps with trial-to-paid conversion tracking",
|
|
220
|
+
maxValue: 63,
|
|
221
|
+
rules: [
|
|
222
|
+
{
|
|
223
|
+
eventName: "app_open",
|
|
224
|
+
conversionValue: 1,
|
|
225
|
+
priority: 1,
|
|
226
|
+
description: "App opened"
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
eventName: "screen_view",
|
|
230
|
+
conditions: { screen_name: "onboarding_complete" },
|
|
231
|
+
conversionValue: 8,
|
|
232
|
+
priority: 3,
|
|
233
|
+
description: "Onboarding completed"
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
eventName: "trial_start",
|
|
237
|
+
conversionValue: 20,
|
|
238
|
+
priority: 5,
|
|
239
|
+
description: "Free trial started"
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
eventName: "purchase_success",
|
|
243
|
+
conditions: { revenue: { "<": 5 } },
|
|
244
|
+
conversionValue: 35,
|
|
245
|
+
priority: 7,
|
|
246
|
+
description: "Small purchase (<$5)"
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
eventName: "subscription_start",
|
|
250
|
+
conversionValue: 50,
|
|
251
|
+
priority: 10,
|
|
252
|
+
description: "Subscription started"
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
eventName: "subscription_renew",
|
|
256
|
+
conversionValue: 63,
|
|
257
|
+
priority: 15,
|
|
258
|
+
description: "Subscription renewed"
|
|
259
|
+
}
|
|
260
|
+
]
|
|
261
|
+
};
|
|
262
|
+
case "engagement": return {
|
|
263
|
+
name: "engagement",
|
|
264
|
+
description: "Optimized for engagement-driven apps focusing on user activity",
|
|
265
|
+
maxValue: 63,
|
|
266
|
+
rules: [
|
|
267
|
+
{
|
|
268
|
+
eventName: "app_open",
|
|
269
|
+
conversionValue: 1,
|
|
270
|
+
priority: 1,
|
|
271
|
+
description: "App opened"
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
eventName: "content_open",
|
|
275
|
+
conversionValue: 5,
|
|
276
|
+
priority: 2,
|
|
277
|
+
description: "Content viewed"
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
eventName: "search",
|
|
281
|
+
conversionValue: 12,
|
|
282
|
+
priority: 4,
|
|
283
|
+
description: "Search performed"
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
eventName: "bookmark_add",
|
|
287
|
+
conversionValue: 25,
|
|
288
|
+
priority: 6,
|
|
289
|
+
description: "Content bookmarked"
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
eventName: "app_open",
|
|
293
|
+
conditions: { session_count: { ">": 3 } },
|
|
294
|
+
conversionValue: 40,
|
|
295
|
+
priority: 8,
|
|
296
|
+
description: "Regular user (3+ sessions)"
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
eventName: "app_open",
|
|
300
|
+
conditions: { session_count: { ">": 10 } },
|
|
301
|
+
conversionValue: 63,
|
|
302
|
+
priority: 12,
|
|
303
|
+
description: "Power user (10+ sessions)"
|
|
304
|
+
}
|
|
305
|
+
]
|
|
306
|
+
};
|
|
307
|
+
case "iap": return {
|
|
308
|
+
name: "iap",
|
|
309
|
+
description: "Optimized for in-app purchase revenue tracking",
|
|
310
|
+
maxValue: 63,
|
|
311
|
+
rules: [
|
|
312
|
+
{
|
|
313
|
+
eventName: "app_open",
|
|
314
|
+
conversionValue: 1,
|
|
315
|
+
priority: 1,
|
|
316
|
+
description: "App opened"
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
eventName: "paywall_show",
|
|
320
|
+
conversionValue: 8,
|
|
321
|
+
priority: 2,
|
|
322
|
+
description: "Paywall shown"
|
|
323
|
+
},
|
|
324
|
+
{
|
|
325
|
+
eventName: "purchase_attempt",
|
|
326
|
+
conversionValue: 15,
|
|
327
|
+
priority: 3,
|
|
328
|
+
description: "Purchase attempted"
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
eventName: "purchase_success",
|
|
332
|
+
conditions: { revenue: { "<": 1 } },
|
|
333
|
+
conversionValue: 25,
|
|
334
|
+
priority: 5,
|
|
335
|
+
description: "Small purchase (<$1)"
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
eventName: "purchase_success",
|
|
339
|
+
conditions: { revenue: {
|
|
340
|
+
">=": 1,
|
|
341
|
+
"<": 10
|
|
342
|
+
} },
|
|
343
|
+
conversionValue: 40,
|
|
344
|
+
priority: 7,
|
|
345
|
+
description: "Medium purchase ($1-$10)"
|
|
346
|
+
},
|
|
347
|
+
{
|
|
348
|
+
eventName: "purchase_success",
|
|
349
|
+
conditions: { revenue: { ">=": 10 } },
|
|
350
|
+
conversionValue: 63,
|
|
351
|
+
priority: 10,
|
|
352
|
+
description: "Large purchase ($10+)"
|
|
353
|
+
}
|
|
354
|
+
]
|
|
355
|
+
};
|
|
356
|
+
default: throw new Error(`Unknown preset: ${preset}`);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Get current preset name
|
|
361
|
+
*/
|
|
362
|
+
getCurrentPreset() {
|
|
363
|
+
return this.currentPreset || null;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Get current rules
|
|
367
|
+
*/
|
|
368
|
+
getCurrentRules() {
|
|
369
|
+
return [...this.rules];
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Get rule evaluation history
|
|
373
|
+
*/
|
|
374
|
+
getEvaluationHistory() {
|
|
375
|
+
return [...this.ruleEvaluationHistory];
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Check if SKAN 4.0 features are available
|
|
379
|
+
*/
|
|
380
|
+
async supportsSKAN4() {
|
|
381
|
+
return await this.getVersion() === "4.0";
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Get SKAN metrics for debugging
|
|
385
|
+
*/
|
|
386
|
+
async getSKANMetrics() {
|
|
387
|
+
const isSupported = await this.isSupported();
|
|
388
|
+
const version = await this.getVersion();
|
|
389
|
+
const currentValue = await this.getCurrentValue();
|
|
390
|
+
const attributionData = await this.getAttributionData();
|
|
391
|
+
return {
|
|
392
|
+
isSupported,
|
|
393
|
+
version,
|
|
394
|
+
currentValue,
|
|
395
|
+
currentPreset: this.currentPreset || null,
|
|
396
|
+
ruleCount: this.rules.length,
|
|
397
|
+
evaluationCount: this.ruleEvaluationHistory.length,
|
|
398
|
+
attributionData,
|
|
399
|
+
lastEvaluations: this.ruleEvaluationHistory.slice(-5)
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
const SKAN = new SKANManager();
|
|
404
|
+
|
|
405
|
+
//#endregion
|
|
406
|
+
//#region src/storage/identity.ts
|
|
407
|
+
const INSTALL_ID_KEY = "layers_install_id";
|
|
408
|
+
let AsyncStorage$1;
|
|
409
|
+
try {
|
|
410
|
+
AsyncStorage$1 = __require("@react-native-async-storage/async-storage").default;
|
|
411
|
+
} catch (_e) {}
|
|
412
|
+
let _pendingPromise = null;
|
|
413
|
+
function getOrSetInstallId() {
|
|
414
|
+
if (_pendingPromise) return _pendingPromise;
|
|
415
|
+
_pendingPromise = (async () => {
|
|
416
|
+
try {
|
|
417
|
+
if (!AsyncStorage$1) {
|
|
418
|
+
console.warn("[Layers] AsyncStorage not found. Using temporary install_id. Identity will not persist across restarts.");
|
|
419
|
+
return generateUUID();
|
|
420
|
+
}
|
|
421
|
+
const existingId = await AsyncStorage$1.getItem(INSTALL_ID_KEY);
|
|
422
|
+
if (existingId) return existingId;
|
|
423
|
+
const newId = generateUUID();
|
|
424
|
+
await AsyncStorage$1.setItem(INSTALL_ID_KEY, newId);
|
|
425
|
+
return newId;
|
|
426
|
+
} catch (error) {
|
|
427
|
+
console.warn("[Layers] Failed to access storage for install_id", error);
|
|
428
|
+
return generateUUID();
|
|
429
|
+
}
|
|
430
|
+
})();
|
|
431
|
+
return _pendingPromise;
|
|
432
|
+
}
|
|
433
|
+
function generateUUID() {
|
|
434
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
435
|
+
const r = Math.random() * 16 | 0;
|
|
436
|
+
return (c === "x" ? r : r & 3 | 8).toString(16);
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
//#endregion
|
|
441
|
+
//#region src/storage/nativeQueueStorage.ts
|
|
442
|
+
const STORAGE_KEY_PREFIX = "layers_queue_";
|
|
443
|
+
let AsyncStorage;
|
|
444
|
+
try {
|
|
445
|
+
AsyncStorage = __require("@react-native-async-storage/async-storage").default;
|
|
446
|
+
} catch (_e) {}
|
|
447
|
+
const createMemoryQueueStorage = () => {
|
|
448
|
+
let snapshot = null;
|
|
449
|
+
return {
|
|
450
|
+
async load() {
|
|
451
|
+
return snapshot;
|
|
452
|
+
},
|
|
453
|
+
async save(nextSnapshot) {
|
|
454
|
+
snapshot = nextSnapshot;
|
|
455
|
+
},
|
|
456
|
+
async clear() {
|
|
457
|
+
snapshot = null;
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
};
|
|
461
|
+
function createReactNativeQueueStorage(appId) {
|
|
462
|
+
if (!AsyncStorage) {
|
|
463
|
+
console.warn("[Layers] AsyncStorage not found. Falling back to in-memory storage. Events will not persist across restarts.");
|
|
464
|
+
return createMemoryQueueStorage();
|
|
465
|
+
}
|
|
466
|
+
const storageKey = `${STORAGE_KEY_PREFIX}${appId}`;
|
|
467
|
+
return {
|
|
468
|
+
async load() {
|
|
469
|
+
try {
|
|
470
|
+
const value = await AsyncStorage.getItem(storageKey);
|
|
471
|
+
if (!value) return null;
|
|
472
|
+
return JSON.parse(value);
|
|
473
|
+
} catch (error) {
|
|
474
|
+
console.warn("[Layers] Failed to load queue from AsyncStorage", error);
|
|
475
|
+
return null;
|
|
476
|
+
}
|
|
477
|
+
},
|
|
478
|
+
async save(snapshot) {
|
|
479
|
+
try {
|
|
480
|
+
await AsyncStorage.setItem(storageKey, JSON.stringify(snapshot));
|
|
481
|
+
} catch (error) {
|
|
482
|
+
console.warn("[Layers] Failed to persist queue to AsyncStorage", error);
|
|
483
|
+
}
|
|
484
|
+
},
|
|
485
|
+
async clear() {
|
|
486
|
+
try {
|
|
487
|
+
await AsyncStorage.removeItem(storageKey);
|
|
488
|
+
} catch (error) {
|
|
489
|
+
console.warn("[Layers] Failed to clear queue storage", error);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
//#endregion
|
|
496
|
+
//#region src/ExpoRouterIntegration.ts
|
|
497
|
+
/**
|
|
498
|
+
* Hook for tracking screen views with Expo Router.
|
|
499
|
+
*
|
|
500
|
+
* @param usePathname - The `usePathname` hook from `expo-router`.
|
|
501
|
+
* @param useGlobalSearchParams - The `useGlobalSearchParams` hook from `expo-router`.
|
|
502
|
+
*/
|
|
503
|
+
function useLayersExpoRouterTracking(usePathname, useGlobalSearchParams) {
|
|
504
|
+
const pathname = usePathname();
|
|
505
|
+
const params = useGlobalSearchParams();
|
|
506
|
+
useEffect(() => {
|
|
507
|
+
if (pathname) {
|
|
508
|
+
const properties = {};
|
|
509
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
510
|
+
if (Array.isArray(value)) properties[key] = value.join(",");
|
|
511
|
+
else properties[key] = value;
|
|
512
|
+
});
|
|
513
|
+
Layers.screen(pathname, properties).catch((err) => {
|
|
514
|
+
console.warn("[Layers] Failed to track screen view:", err);
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
}, [pathname, JSON.stringify(params)]);
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Component for tracking screen views with Expo Router.
|
|
521
|
+
* Render this in your root `_layout.tsx`.
|
|
522
|
+
*/
|
|
523
|
+
function LayersExpoRouter({ usePathname, useGlobalSearchParams }) {
|
|
524
|
+
useLayersExpoRouterTracking(usePathname, useGlobalSearchParams);
|
|
525
|
+
return null;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
//#endregion
|
|
529
|
+
//#region src/integrations/Commerce.ts
|
|
530
|
+
const Commerce = { trackPurchase: async (transaction) => {
|
|
531
|
+
try {
|
|
532
|
+
await Layers.track("purchase_success", {
|
|
533
|
+
product_id: transaction.products.map((p) => p.productId).join(","),
|
|
534
|
+
transaction_id: transaction.transactionId,
|
|
535
|
+
store: transaction.store,
|
|
536
|
+
revenue: transaction.revenue,
|
|
537
|
+
currency: transaction.currency,
|
|
538
|
+
quantity: transaction.products.reduce((acc, p) => acc + (p.quantity ?? 1), 0),
|
|
539
|
+
items: transaction.products
|
|
540
|
+
});
|
|
541
|
+
} catch (e) {
|
|
542
|
+
console.warn("[Layers] Failed to track purchase:", e);
|
|
543
|
+
}
|
|
544
|
+
} };
|
|
545
|
+
|
|
546
|
+
//#endregion
|
|
547
|
+
//#region src/integrations/RevenueCat.ts
|
|
548
|
+
/**
|
|
549
|
+
* Safe wrapper for RevenueCat integration.
|
|
550
|
+
* Does not import 'react-native-purchases' to avoid runtime crashes.
|
|
551
|
+
*/
|
|
552
|
+
const RevenueCat = {
|
|
553
|
+
connect: (purchasesInstance) => {
|
|
554
|
+
if (_isConnected) return;
|
|
555
|
+
try {
|
|
556
|
+
if (!purchasesInstance || typeof purchasesInstance.addCustomerInfoUpdateListener !== "function" || typeof purchasesInstance.getCustomerInfo !== "function") {
|
|
557
|
+
console.warn("[Layers] RevenueCat.connect() was passed an invalid object. Expected the default export from \"react-native-purchases\".");
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
const purchases = purchasesInstance;
|
|
561
|
+
purchases.addCustomerInfoUpdateListener((info) => {
|
|
562
|
+
handleCustomerInfoUpdate(info, false);
|
|
563
|
+
});
|
|
564
|
+
purchases.getCustomerInfo().then((info) => handleCustomerInfoUpdate(info, true)).catch(() => {});
|
|
565
|
+
_isConnected = true;
|
|
566
|
+
console.log("[Layers] RevenueCat integration connected.");
|
|
567
|
+
} catch (e) {
|
|
568
|
+
console.warn("[Layers] Failed to connect RevenueCat:", e);
|
|
569
|
+
}
|
|
570
|
+
},
|
|
571
|
+
trackPurchase: async (rcPackage) => {
|
|
572
|
+
try {
|
|
573
|
+
if (!rcPackage || !rcPackage.product) return;
|
|
574
|
+
const { product } = rcPackage;
|
|
575
|
+
const store = Platform.OS === "ios" ? "app_store" : Platform.OS === "android" ? "play_store" : "custom";
|
|
576
|
+
await Layers.track("purchase_success", {
|
|
577
|
+
product_id: product.identifier,
|
|
578
|
+
price: product.price,
|
|
579
|
+
currency: product.currencyCode,
|
|
580
|
+
store,
|
|
581
|
+
transaction_id: "",
|
|
582
|
+
source: "revenuecat"
|
|
583
|
+
});
|
|
584
|
+
} catch (e) {}
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
let _activeSubscriptionsSet = /* @__PURE__ */ new Set();
|
|
588
|
+
let _isInitialized = false;
|
|
589
|
+
let _isConnected = false;
|
|
590
|
+
function handleCustomerInfoUpdate(info, isInitialLoad) {
|
|
591
|
+
try {
|
|
592
|
+
const currentSubscriptions = new Set(info.activeSubscriptions || []);
|
|
593
|
+
if (isInitialLoad && _isInitialized) return;
|
|
594
|
+
if (!isInitialLoad && _isInitialized) {
|
|
595
|
+
for (const subId of currentSubscriptions) if (!_activeSubscriptionsSet.has(subId)) Layers.track("subscription_start", {
|
|
596
|
+
product_id: subId,
|
|
597
|
+
platform: "revenuecat"
|
|
598
|
+
}).catch((err) => {
|
|
599
|
+
console.warn("[Layers] Failed to track subscription start:", err);
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
_activeSubscriptionsSet = currentSubscriptions;
|
|
603
|
+
_isInitialized = true;
|
|
604
|
+
const userProps = { is_subscriber: currentSubscriptions.size > 0 };
|
|
605
|
+
if (info.originalAppUserId) userProps.revenuecat_original_app_user_id = info.originalAppUserId;
|
|
606
|
+
Layers.setUserProperties(userProps).catch((err) => {
|
|
607
|
+
console.warn("[Layers] Failed to sync user properties:", err);
|
|
608
|
+
});
|
|
609
|
+
} catch (e) {
|
|
610
|
+
console.warn("[Layers] Error processing RevenueCat update:", e);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
//#endregion
|
|
615
|
+
//#region src/integrations/Superwall.ts
|
|
616
|
+
const Superwall = {
|
|
617
|
+
trackPresentation: async (paywallInfo) => {
|
|
618
|
+
try {
|
|
619
|
+
if (!paywallInfo) return;
|
|
620
|
+
await Layers.track("paywall_show", {
|
|
621
|
+
paywall_id: paywallInfo.identifier,
|
|
622
|
+
placement: paywallInfo.name || "unknown",
|
|
623
|
+
ab_test: paywallInfo.experiment ? {
|
|
624
|
+
id: paywallInfo.experiment.id,
|
|
625
|
+
variant: paywallInfo.experiment.variantId
|
|
626
|
+
} : void 0,
|
|
627
|
+
url: paywallInfo.url
|
|
628
|
+
});
|
|
629
|
+
} catch (e) {}
|
|
630
|
+
},
|
|
631
|
+
trackDismiss: async (paywallInfo) => {
|
|
632
|
+
try {
|
|
633
|
+
if (!paywallInfo) return;
|
|
634
|
+
await Layers.track("paywall_dismiss", {
|
|
635
|
+
paywall_id: paywallInfo.identifier,
|
|
636
|
+
reason: "user_close"
|
|
637
|
+
});
|
|
638
|
+
} catch (e) {}
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
//#endregion
|
|
6
643
|
//#region src/index.ts
|
|
7
644
|
const SUPPORTED_NETWORKS = [
|
|
8
645
|
"meta",
|
|
@@ -55,10 +692,12 @@ var LayersRN = class {
|
|
|
55
692
|
client = null;
|
|
56
693
|
isInitialized = false;
|
|
57
694
|
deepLinkConfig = null;
|
|
695
|
+
skanManager = null;
|
|
58
696
|
async init(config) {
|
|
697
|
+
const installId = await getOrSetInstallId();
|
|
59
698
|
this.client = new LayersClient({
|
|
60
699
|
...config,
|
|
61
|
-
queueStorage:
|
|
700
|
+
queueStorage: createReactNativeQueueStorage(config.appId),
|
|
62
701
|
queueOptions: {
|
|
63
702
|
flushIntervalMs: 1e3,
|
|
64
703
|
maxQueueSize: 50,
|
|
@@ -72,11 +711,65 @@ var LayersRN = class {
|
|
|
72
711
|
});
|
|
73
712
|
this.client.setDeviceInfo({
|
|
74
713
|
platform: Platform.OS === "ios" ? "ios" : Platform.OS === "android" ? "android" : "react-native",
|
|
75
|
-
os_version: typeof Platform.Version === "string" ? Platform.Version : String(Platform.Version)
|
|
714
|
+
os_version: typeof Platform.Version === "string" ? Platform.Version : String(Platform.Version),
|
|
715
|
+
install_id: installId
|
|
76
716
|
});
|
|
77
717
|
await this.client.init();
|
|
718
|
+
if (Platform.OS === "ios") await this.initializeSKAN();
|
|
78
719
|
this.isInitialized = true;
|
|
79
|
-
if (config.enableDebug) console.log("Layers initialized
|
|
720
|
+
if (config.enableDebug) console.log("Layers initialized with SKAN support.");
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* Initialize SKAN and apply config from server
|
|
724
|
+
*/
|
|
725
|
+
async initializeSKAN() {
|
|
726
|
+
try {
|
|
727
|
+
this.skanManager = new SKANManager((data) => {
|
|
728
|
+
if (this.client?.getConfig().enableDebug) console.log("[Layers SKAN] Conversion updated:", {
|
|
729
|
+
event: data.event,
|
|
730
|
+
previousValue: data.previousValue,
|
|
731
|
+
newValue: data.newValue,
|
|
732
|
+
rule: data.matchedRule?.description
|
|
733
|
+
});
|
|
734
|
+
});
|
|
735
|
+
await this.skanManager.initialize();
|
|
736
|
+
const remoteConfig = await this.getRemoteConfig();
|
|
737
|
+
if (remoteConfig?.skan) await this.applySKANConfig(remoteConfig.skan);
|
|
738
|
+
} catch (error) {
|
|
739
|
+
console.warn("[Layers] SKAN initialization failed:", error);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Get remote config from client
|
|
744
|
+
*/
|
|
745
|
+
async getRemoteConfig() {
|
|
746
|
+
if (!this.client) return null;
|
|
747
|
+
try {
|
|
748
|
+
return this.client.remoteConfig ?? null;
|
|
749
|
+
} catch {
|
|
750
|
+
return null;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Apply SKAN configuration from server
|
|
755
|
+
*/
|
|
756
|
+
async applySKANConfig(skanConfig) {
|
|
757
|
+
if (!this.skanManager || !skanConfig.enabled) return;
|
|
758
|
+
try {
|
|
759
|
+
if (skanConfig.preset && [
|
|
760
|
+
"subscriptions",
|
|
761
|
+
"engagement",
|
|
762
|
+
"iap"
|
|
763
|
+
].includes(skanConfig.preset)) {
|
|
764
|
+
this.skanManager.setPreset(skanConfig.preset);
|
|
765
|
+
console.log(`[Layers SKAN] Preset applied: ${skanConfig.preset}`);
|
|
766
|
+
} else if (Array.isArray(skanConfig.customRules) && skanConfig.customRules.length > 0) {
|
|
767
|
+
this.skanManager.setCustomRules(skanConfig.customRules);
|
|
768
|
+
console.log(`[Layers SKAN] Custom rules applied: ${skanConfig.customRules.length} rules`);
|
|
769
|
+
}
|
|
770
|
+
} catch (error) {
|
|
771
|
+
console.warn("[Layers] Failed to apply SKAN config:", error);
|
|
772
|
+
}
|
|
80
773
|
}
|
|
81
774
|
getClient() {
|
|
82
775
|
return this.client;
|
|
@@ -93,9 +786,13 @@ var LayersRN = class {
|
|
|
93
786
|
}
|
|
94
787
|
async track(eventName, properties = {}) {
|
|
95
788
|
if (!this.client) throw new Error("Layers must be initialized before tracking events");
|
|
96
|
-
|
|
789
|
+
const eventProperties = {
|
|
97
790
|
platform: Platform.OS,
|
|
98
791
|
...properties
|
|
792
|
+
};
|
|
793
|
+
await this.client.track(eventName, eventProperties);
|
|
794
|
+
if (Platform.OS === "ios" && this.skanManager) this.skanManager.processEvent(eventName, eventProperties).catch((error) => {
|
|
795
|
+
if (this.client?.getConfig().enableDebug) console.warn("[Layers SKAN] Event processing failed:", error);
|
|
99
796
|
});
|
|
100
797
|
}
|
|
101
798
|
async screen(screenName, properties = {}) {
|
|
@@ -108,6 +805,14 @@ var LayersRN = class {
|
|
|
108
805
|
if (!this.client) throw new Error("Layers must be initialized before setting user properties");
|
|
109
806
|
await this.client.setUserProperties(properties);
|
|
110
807
|
}
|
|
808
|
+
setAppUserId(appUserId) {
|
|
809
|
+
if (!this.client) throw new Error("Layers must be initialized before setting app user ID");
|
|
810
|
+
this.client.setAppUserId(appUserId);
|
|
811
|
+
}
|
|
812
|
+
clearAppUserId() {
|
|
813
|
+
if (!this.client) throw new Error("Layers must be initialized before clearing app user ID");
|
|
814
|
+
this.client.setAppUserId(void 0);
|
|
815
|
+
}
|
|
111
816
|
async setConsent(consent) {
|
|
112
817
|
if (!this.client) throw new Error("Layers must be initialized before setting consent");
|
|
113
818
|
await this.client.setConsent(consent);
|
|
@@ -122,7 +827,10 @@ var LayersRN = class {
|
|
|
122
827
|
hideOverlay: () => {},
|
|
123
828
|
isOverlayVisible: () => false,
|
|
124
829
|
getATTStatus: async () => NOT_SUPPORTED_ATT_STATUS,
|
|
125
|
-
getSKANMetrics: async () =>
|
|
830
|
+
getSKANMetrics: async () => {
|
|
831
|
+
if (Platform.OS !== "ios" || !this.skanManager) return NOT_SUPPORTED_SKAN_METRICS;
|
|
832
|
+
return this.skanManager.getSKANMetrics();
|
|
833
|
+
},
|
|
126
834
|
getDeepLinkMetrics: async () => EMPTY_DEEP_LINK_METRICS,
|
|
127
835
|
getConnectorStatuses: async () => this.createConnectorStatusMap(),
|
|
128
836
|
testConnectorConnection: async (_network) => false,
|
|
@@ -195,14 +903,36 @@ var LayersRN = class {
|
|
|
195
903
|
}
|
|
196
904
|
};
|
|
197
905
|
skan = {
|
|
198
|
-
setPreset: async (
|
|
199
|
-
|
|
906
|
+
setPreset: async (preset) => {
|
|
907
|
+
if (Platform.OS !== "ios") {
|
|
908
|
+
console.warn("[Layers SKAN] Only supported on iOS");
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
if (!this.skanManager) {
|
|
912
|
+
console.warn("[Layers SKAN] Not initialized");
|
|
913
|
+
return;
|
|
914
|
+
}
|
|
915
|
+
this.skanManager.setPreset(preset);
|
|
916
|
+
},
|
|
917
|
+
setRules: async (rules) => {
|
|
918
|
+
if (Platform.OS !== "ios") {
|
|
919
|
+
console.warn("[Layers SKAN] Only supported on iOS");
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
if (!this.skanManager) {
|
|
923
|
+
console.warn("[Layers SKAN] Not initialized");
|
|
924
|
+
return;
|
|
925
|
+
}
|
|
926
|
+
this.skanManager.setCustomRules(rules);
|
|
200
927
|
},
|
|
201
|
-
|
|
202
|
-
|
|
928
|
+
getCurrentConversionValue: async () => {
|
|
929
|
+
if (Platform.OS !== "ios" || !this.skanManager) return null;
|
|
930
|
+
return this.skanManager.getCurrentValue();
|
|
203
931
|
},
|
|
204
|
-
|
|
205
|
-
|
|
932
|
+
getMetrics: async () => {
|
|
933
|
+
if (Platform.OS !== "ios" || !this.skanManager) return NOT_SUPPORTED_SKAN_METRICS;
|
|
934
|
+
return this.skanManager.getSKANMetrics();
|
|
935
|
+
}
|
|
206
936
|
};
|
|
207
937
|
createConnectorStatusMap() {
|
|
208
938
|
const statuses = {};
|
|
@@ -226,5 +956,5 @@ var src_default = Layers;
|
|
|
226
956
|
const DebugOverlay = (_props) => null;
|
|
227
957
|
|
|
228
958
|
//#endregion
|
|
229
|
-
export { DebugOverlay, Layers, src_default as default };
|
|
959
|
+
export { Commerce, DebugOverlay, Layers, LayersExpoRouter, RevenueCat, Superwall, src_default as default, useLayersExpoRouterTracking };
|
|
230
960
|
//# sourceMappingURL=index.js.map
|