@amplytools/react-native-amply-sdk 0.14.0 → 0.15.1
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/AmplyReactNative.podspec +1 -1
- package/README.md +41 -0
- package/android/build.gradle +1 -1
- package/android/src/main/java/tools/amply/sdk/reactnative/AmplyModule.kt +22 -0
- package/android/src/main/java/tools/amply/sdk/reactnative/core/AmplyClient.kt +11 -0
- package/android/src/main/java/tools/amply/sdk/reactnative/core/DefaultAmplyClient.kt +29 -0
- package/android/src/main/java/tools/amply/sdk/reactnative/model/CampaignTouchPayload.kt +12 -0
- package/android/src/newarch/java/tools/amply/sdk/reactnative/NativeAmplyModuleSpec.java +8 -0
- package/android/src/newarch/jni/AmplyReactNative-generated.cpp +12 -0
- package/dist/cjs/campaignTouch.js +44 -0
- package/dist/cjs/campaignTouch.js.map +1 -0
- package/dist/cjs/hooks/useAmplySystemEvents.js +60 -0
- package/dist/cjs/hooks/useAmplySystemEvents.js.map +1 -0
- package/dist/cjs/index.js +723 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/nativeModule.js +55 -0
- package/dist/cjs/nativeModule.js.map +1 -0
- package/dist/cjs/nativeSpecs/NativeAmplyModule.js +5 -0
- package/dist/cjs/nativeSpecs/NativeAmplyModule.js.map +1 -0
- package/dist/cjs/systemEventUtils.js +47 -0
- package/dist/cjs/systemEventUtils.js.map +1 -0
- package/dist/cjs/systemEvents.js +27 -0
- package/dist/cjs/systemEvents.js.map +1 -0
- package/dist/src/campaignTouch.d.ts +36 -0
- package/dist/src/campaignTouch.d.ts.map +1 -0
- package/dist/src/campaignTouch.js +39 -0
- package/dist/src/index.d.ts +35 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +120 -0
- package/dist/src/nativeSpecs/NativeAmplyModule.d.ts +28 -0
- package/dist/src/nativeSpecs/NativeAmplyModule.d.ts.map +1 -1
- package/dist/src/systemEventUtils.d.ts.map +1 -1
- package/dist/src/systemEventUtils.js +3 -1
- package/docs/ARCHITECTURE.md +28 -19
- package/docs/DEVELOPMENT.md +2 -1
- package/ios/Sources/AmplyReactNative/AmplyModule.mm +56 -0
- package/ios/Sources/AmplyReactNative/AmplyReactNative/AmplyReactNative-generated.mm +14 -0
- package/ios/Sources/AmplyReactNative/AmplyReactNative/AmplyReactNative.h +4 -0
- package/package.json +12 -6
- package/scripts/check-package.js +159 -0
- package/src/campaignTouch.ts +74 -0
- package/src/index.ts +124 -0
- package/src/nativeSpecs/NativeAmplyModule.ts +29 -0
- package/src/systemEventUtils.ts +3 -1
- package/dist/index.js +0 -272
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -234
- package/dist/index.mjs.map +0 -1
- package/dist/plugin/index.d.ts +0 -6
- package/dist/plugin/index.d.ts.map +0 -1
- package/dist/plugin/index.js +0 -186
- package/dist/plugin/index.js.map +0 -1
- package/dist/plugin/index.mjs +0 -169
- package/dist/plugin/index.mjs.map +0 -1
- package/dist/plugin/withAmply.d.ts +0 -12
- package/dist/plugin/withAmply.d.ts.map +0 -1
- package/dist/src/__tests__/appLanguage.test.d.ts +0 -43
- package/dist/src/__tests__/appLanguage.test.d.ts.map +0 -1
- package/dist/src/__tests__/appLanguage.test.js +0 -211
- package/dist/src/__tests__/campaignResolved.test.d.ts +0 -2
- package/dist/src/__tests__/campaignResolved.test.d.ts.map +0 -1
- package/dist/src/__tests__/campaignResolved.test.js +0 -208
- package/dist/src/__tests__/gateRegistrationLifecycle.test.d.ts +0 -2
- package/dist/src/__tests__/gateRegistrationLifecycle.test.d.ts.map +0 -1
- package/dist/src/__tests__/gateRegistrationLifecycle.test.js +0 -215
- package/dist/src/__tests__/index.test.d.ts +0 -2
- package/dist/src/__tests__/index.test.d.ts.map +0 -1
- package/dist/src/__tests__/index.test.js +0 -204
- package/dist/src/__tests__/systemEventUtils.test.d.ts +0 -2
- package/dist/src/__tests__/systemEventUtils.test.d.ts.map +0 -1
- package/dist/src/__tests__/systemEventUtils.test.js +0 -56
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/plugin/tsconfig.tsbuildinfo +0 -1
- package/src/__tests__/appLanguage.test.ts +0 -296
- package/src/__tests__/campaignResolved.test.ts +0 -294
- package/src/__tests__/gateRegistrationLifecycle.test.ts +0 -281
- package/src/__tests__/index.test.ts +0 -258
- package/src/__tests__/systemEventUtils.test.ts +0 -75
|
@@ -1,258 +0,0 @@
|
|
|
1
|
-
import * as Amply from '../index';
|
|
2
|
-
import {__setNativeModule} from '../nativeModule';
|
|
3
|
-
|
|
4
|
-
const mockNativeModule = {
|
|
5
|
-
initialize: jest.fn(),
|
|
6
|
-
isInitialized: jest.fn(() => true),
|
|
7
|
-
track: jest.fn(),
|
|
8
|
-
getRecentEvents: jest.fn(),
|
|
9
|
-
getDataSetSnapshot: jest.fn(),
|
|
10
|
-
registerDeepLinkListener: jest.fn(),
|
|
11
|
-
addListener: jest.fn(),
|
|
12
|
-
removeListeners: jest.fn(),
|
|
13
|
-
onSystemEvent: jest.fn(),
|
|
14
|
-
onDeepLink: jest.fn(),
|
|
15
|
-
setCustomProperties: jest.fn(),
|
|
16
|
-
getCustomProperty: jest.fn(),
|
|
17
|
-
removeCustomProperty: jest.fn(),
|
|
18
|
-
clearCustomProperties: jest.fn(),
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
describe('Amply JS API', () => {
|
|
22
|
-
afterEach(() => {
|
|
23
|
-
jest.clearAllMocks();
|
|
24
|
-
__setNativeModule(null);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
beforeEach(() => {
|
|
28
|
-
__setNativeModule(mockNativeModule as never);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('initializes the native module', async () => {
|
|
32
|
-
await Amply.initialize({ appId: 'id', apiKeyPublic: 'public', apiKeySecret: 'secret' });
|
|
33
|
-
|
|
34
|
-
expect(mockNativeModule.initialize).toHaveBeenCalledWith(
|
|
35
|
-
expect.objectContaining({ appId: 'id', apiKeyPublic: 'public' })
|
|
36
|
-
);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
// A missing key used to reach the native SDK, whose config builder threw an exception that
|
|
40
|
-
// crossed into Objective-C uncaught and aborted the host app — no JS try/catch could stop it.
|
|
41
|
-
// Bad keys must cost the app its Amply, never its process.
|
|
42
|
-
describe.each([
|
|
43
|
-
['apiKeySecret', { appId: 'id', apiKeyPublic: 'public' }],
|
|
44
|
-
['apiKeySecret', { appId: 'id', apiKeyPublic: 'public', apiKeySecret: '' }],
|
|
45
|
-
['apiKeySecret', { appId: 'id', apiKeyPublic: 'public', apiKeySecret: ' ' }],
|
|
46
|
-
['apiKeySecret', { appId: 'id', apiKeyPublic: 'public', apiKeySecret: null }],
|
|
47
|
-
['apiKeyPublic', { appId: 'id', apiKeyPublic: '', apiKeySecret: 'secret' }],
|
|
48
|
-
['appId', { appId: '', apiKeyPublic: 'public', apiKeySecret: 'secret' }],
|
|
49
|
-
])('when %s is missing (%j)', (field, config) => {
|
|
50
|
-
it('rejects with AMP_INVALID_CONFIG, names the field and never calls the native module', async () => {
|
|
51
|
-
const error = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
52
|
-
|
|
53
|
-
await expect(Amply.initialize(config as never)).rejects.toMatchObject({
|
|
54
|
-
code: 'AMP_INVALID_CONFIG',
|
|
55
|
-
message: expect.stringContaining(field),
|
|
56
|
-
});
|
|
57
|
-
expect(mockNativeModule.initialize).not.toHaveBeenCalled();
|
|
58
|
-
expect(error).toHaveBeenCalledWith(expect.stringContaining(field));
|
|
59
|
-
|
|
60
|
-
error.mockRestore();
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('passes the base-URL overrides through to the native layer', async () => {
|
|
65
|
-
await Amply.initialize({
|
|
66
|
-
appId: 'id',
|
|
67
|
-
apiKeyPublic: 'public',
|
|
68
|
-
apiKeySecret: 'secret',
|
|
69
|
-
configBaseUrl: 'https://config-staging.example.com',
|
|
70
|
-
backendBaseUrl: 'https://api-staging.example.com',
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
expect(mockNativeModule.initialize).toHaveBeenCalledWith(
|
|
74
|
-
expect.objectContaining({
|
|
75
|
-
configBaseUrl: 'https://config-staging.example.com',
|
|
76
|
-
backendBaseUrl: 'https://api-staging.example.com',
|
|
77
|
-
})
|
|
78
|
-
);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// `endpoint` is the older single-URL spelling. It stays accepted so existing
|
|
82
|
-
// callers keep working, but it only ever meant the backend host — the campaign
|
|
83
|
-
// manifest is served from a different one.
|
|
84
|
-
it('still accepts the legacy endpoint spelling', async () => {
|
|
85
|
-
await Amply.initialize({
|
|
86
|
-
appId: 'id',
|
|
87
|
-
apiKeyPublic: 'public',
|
|
88
|
-
apiKeySecret: 'secret',
|
|
89
|
-
endpoint: 'https://api-staging.example.com',
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
expect(mockNativeModule.initialize).toHaveBeenCalledWith(
|
|
93
|
-
expect.objectContaining({ endpoint: 'https://api-staging.example.com' })
|
|
94
|
-
);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it('tracks events through the native layer', async () => {
|
|
98
|
-
await Amply.track({ name: 'Test Event', properties: { foo: 'bar' } });
|
|
99
|
-
|
|
100
|
-
expect(mockNativeModule.track).toHaveBeenCalledWith({
|
|
101
|
-
name: 'Test Event',
|
|
102
|
-
properties: { foo: 'bar' },
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('reads recent events', async () => {
|
|
107
|
-
const events = [{ name: 'e1' }];
|
|
108
|
-
mockNativeModule.getRecentEvents.mockResolvedValueOnce(events);
|
|
109
|
-
|
|
110
|
-
const result = await Amply.getRecentEvents(3);
|
|
111
|
-
|
|
112
|
-
expect(mockNativeModule.getRecentEvents).toHaveBeenCalledWith(3);
|
|
113
|
-
expect(result).toBe(events);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it('requests dataset snapshots', async () => {
|
|
117
|
-
const snapshot = { foo: 'bar' };
|
|
118
|
-
mockNativeModule.getDataSetSnapshot.mockResolvedValueOnce(snapshot);
|
|
119
|
-
|
|
120
|
-
const type = { kind: '@device' as const };
|
|
121
|
-
const result = await Amply.getDataSetSnapshot(type);
|
|
122
|
-
|
|
123
|
-
expect(mockNativeModule.getDataSetSnapshot).toHaveBeenCalledWith(type);
|
|
124
|
-
expect(result).toBe(snapshot);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it.each([
|
|
128
|
-
['singleton @device', { kind: '@device' as const }],
|
|
129
|
-
['singleton @user', { kind: '@user' as const }],
|
|
130
|
-
['singleton @custom', { kind: '@custom' as const }],
|
|
131
|
-
['singleton @session', { kind: '@session' as const }],
|
|
132
|
-
[
|
|
133
|
-
'@triggeredEvent with global strategy',
|
|
134
|
-
{
|
|
135
|
-
kind: '@triggeredEvent' as const,
|
|
136
|
-
data: {
|
|
137
|
-
countStrategy: 'global' as const,
|
|
138
|
-
params: [{ name: 'level', value: 5 }],
|
|
139
|
-
eventName: 'LevelComplete',
|
|
140
|
-
},
|
|
141
|
-
},
|
|
142
|
-
],
|
|
143
|
-
[
|
|
144
|
-
'@triggeredEvent with session strategy and no eventName',
|
|
145
|
-
{
|
|
146
|
-
kind: '@triggeredEvent' as const,
|
|
147
|
-
data: {
|
|
148
|
-
countStrategy: 'session' as const,
|
|
149
|
-
params: [],
|
|
150
|
-
},
|
|
151
|
-
},
|
|
152
|
-
],
|
|
153
|
-
[
|
|
154
|
-
'@events with mixed event types',
|
|
155
|
-
{
|
|
156
|
-
kind: '@events' as const,
|
|
157
|
-
data: [
|
|
158
|
-
{ name: 'Purchase', type: 'custom' as const, params: [{ name: 'sku', value: 'ABC' }] },
|
|
159
|
-
{ name: 'SessionStarted', type: 'system' as const, params: [] },
|
|
160
|
-
],
|
|
161
|
-
},
|
|
162
|
-
],
|
|
163
|
-
])('passes %s to the native bridge unchanged', async (_label, type) => {
|
|
164
|
-
mockNativeModule.getDataSetSnapshot.mockResolvedValueOnce({});
|
|
165
|
-
|
|
166
|
-
await Amply.getDataSetSnapshot(type);
|
|
167
|
-
|
|
168
|
-
expect(mockNativeModule.getDataSetSnapshot).toHaveBeenCalledWith(type);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it('subscribes to system events through the TurboModule emitter', async () => {
|
|
172
|
-
const remove = jest.fn();
|
|
173
|
-
mockNativeModule.onSystemEvent.mockReturnValueOnce({ remove });
|
|
174
|
-
|
|
175
|
-
const listener = jest.fn();
|
|
176
|
-
const unsubscribe = await Amply.addSystemEventListener(listener);
|
|
177
|
-
|
|
178
|
-
expect(mockNativeModule.onSystemEvent).toHaveBeenCalledTimes(1);
|
|
179
|
-
|
|
180
|
-
// The native listener is a wrapper that filters DebugLog events
|
|
181
|
-
const nativeListener = mockNativeModule.onSystemEvent.mock.calls[0][0];
|
|
182
|
-
|
|
183
|
-
// DebugLog events should be filtered out
|
|
184
|
-
nativeListener({ name: 'DebugLog', type: 'log', timestamp: 1, properties: {} });
|
|
185
|
-
expect(listener).not.toHaveBeenCalled();
|
|
186
|
-
|
|
187
|
-
// Other events should pass through
|
|
188
|
-
const systemEvent = { name: 'SessionStart', type: 'system', timestamp: 2, properties: {} };
|
|
189
|
-
nativeListener(systemEvent);
|
|
190
|
-
expect(listener).toHaveBeenCalledWith(systemEvent);
|
|
191
|
-
|
|
192
|
-
unsubscribe();
|
|
193
|
-
expect(remove).toHaveBeenCalledTimes(1);
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
it('sets a single custom property via setCustomProperties', () => {
|
|
197
|
-
Amply.setCustomProperty('plan', 'premium');
|
|
198
|
-
|
|
199
|
-
expect(mockNativeModule.setCustomProperties).toHaveBeenCalledWith({
|
|
200
|
-
plan: 'premium',
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('sets multiple custom properties at once', () => {
|
|
205
|
-
Amply.setCustomProperties({ plan: 'premium', age: 25, active: true });
|
|
206
|
-
|
|
207
|
-
expect(mockNativeModule.setCustomProperties).toHaveBeenCalledWith({
|
|
208
|
-
plan: 'premium',
|
|
209
|
-
age: 25,
|
|
210
|
-
active: true,
|
|
211
|
-
});
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
it('gets a custom property value', async () => {
|
|
215
|
-
mockNativeModule.getCustomProperty.mockResolvedValueOnce({ value: 'premium' });
|
|
216
|
-
|
|
217
|
-
const result = await Amply.getCustomProperty('plan');
|
|
218
|
-
|
|
219
|
-
expect(mockNativeModule.getCustomProperty).toHaveBeenCalledWith('plan');
|
|
220
|
-
expect(result).toBe('premium');
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
it('returns null for a missing custom property', async () => {
|
|
224
|
-
mockNativeModule.getCustomProperty.mockResolvedValueOnce({});
|
|
225
|
-
|
|
226
|
-
const result = await Amply.getCustomProperty('nonexistent');
|
|
227
|
-
|
|
228
|
-
expect(result).toBeNull();
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
it('removes a custom property', () => {
|
|
232
|
-
Amply.removeCustomProperty('plan');
|
|
233
|
-
|
|
234
|
-
expect(mockNativeModule.removeCustomProperty).toHaveBeenCalledWith('plan');
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
it('clears all custom properties', () => {
|
|
238
|
-
Amply.clearCustomProperties();
|
|
239
|
-
|
|
240
|
-
expect(mockNativeModule.clearCustomProperties).toHaveBeenCalled();
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
it('registers deep links once and subscribes via the TurboModule emitter', async () => {
|
|
244
|
-
const remove = jest.fn();
|
|
245
|
-
mockNativeModule.onDeepLink.mockReturnValue({ remove });
|
|
246
|
-
|
|
247
|
-
const listener = jest.fn();
|
|
248
|
-
const unsubscribe = await Amply.addDeepLinkListener(listener);
|
|
249
|
-
expect(mockNativeModule.registerDeepLinkListener).toHaveBeenCalledTimes(1);
|
|
250
|
-
expect(mockNativeModule.onDeepLink).toHaveBeenCalledWith(listener);
|
|
251
|
-
|
|
252
|
-
await Amply.addDeepLinkListener(() => {});
|
|
253
|
-
expect(mockNativeModule.registerDeepLinkListener).toHaveBeenCalledTimes(1);
|
|
254
|
-
|
|
255
|
-
unsubscribe();
|
|
256
|
-
expect(remove).toHaveBeenCalledTimes(1);
|
|
257
|
-
});
|
|
258
|
-
});
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import {formatSystemEventLabel} from '../systemEventUtils';
|
|
2
|
-
import type {EventRecord} from '../nativeSpecs/NativeAmplyModule';
|
|
3
|
-
|
|
4
|
-
function makeEvent(
|
|
5
|
-
name: string,
|
|
6
|
-
properties: Record<string, unknown> = {},
|
|
7
|
-
): EventRecord {
|
|
8
|
-
return {name, type: 'system', timestamp: Date.now(), properties};
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
describe('formatSystemEventLabel', () => {
|
|
12
|
-
describe('SessionStarted', () => {
|
|
13
|
-
it('returns "Session started (cold)" for cold start', () => {
|
|
14
|
-
const event = makeEvent('SessionStarted', {type: 'cold'});
|
|
15
|
-
expect(formatSystemEventLabel(event)).toBe('Session started (cold)');
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('returns "Session started (warm)" for warm start', () => {
|
|
19
|
-
const event = makeEvent('SessionStarted', {type: 'warm'});
|
|
20
|
-
expect(formatSystemEventLabel(event)).toBe('Session started (warm)');
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('returns "Session started" when type is absent', () => {
|
|
24
|
-
const event = makeEvent('SessionStarted');
|
|
25
|
-
expect(formatSystemEventLabel(event)).toBe('Session started');
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('handles unexpected type values gracefully', () => {
|
|
29
|
-
const event = makeEvent('SessionStarted', {type: 'unknown'});
|
|
30
|
-
expect(formatSystemEventLabel(event)).toBe('Session started (unknown)');
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
describe('other events', () => {
|
|
35
|
-
it('formats CampaignShown with campaignId and source', () => {
|
|
36
|
-
const event = makeEvent('CampaignShown', {
|
|
37
|
-
campaignId: 'abc',
|
|
38
|
-
source: 'click',
|
|
39
|
-
});
|
|
40
|
-
expect(formatSystemEventLabel(event)).toBe(
|
|
41
|
-
'Campaign shown (abc) via click',
|
|
42
|
-
);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('formats EventTriggered with source event', () => {
|
|
46
|
-
const event = makeEvent('EventTriggered', {sourceEvent: 'purchase'});
|
|
47
|
-
expect(formatSystemEventLabel(event)).toBe('Event triggered (purchase)');
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('formats ConfigFetchFinished with campaign count', () => {
|
|
51
|
-
const event = makeEvent('ConfigFetchFinished', {
|
|
52
|
-
success: true,
|
|
53
|
-
campaignCount: 3,
|
|
54
|
-
});
|
|
55
|
-
expect(formatSystemEventLabel(event)).toBe(
|
|
56
|
-
'Config fetch finished (3 campaigns)',
|
|
57
|
-
);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('formats ConfigFetchFinished failure', () => {
|
|
61
|
-
const event = makeEvent('ConfigFetchFinished', {success: false});
|
|
62
|
-
expect(formatSystemEventLabel(event)).toBe('Config fetch failed');
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('returns default label for SessionFinished', () => {
|
|
66
|
-
const event = makeEvent('SessionFinished');
|
|
67
|
-
expect(formatSystemEventLabel(event)).toBe('Session finished');
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('returns fallback for unknown events', () => {
|
|
71
|
-
const event = makeEvent('UnknownEvent');
|
|
72
|
-
expect(formatSystemEventLabel(event)).toBe('System event UnknownEvent');
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
});
|