Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.3.3

- **Fixed:** `setAppUserId` posted `/ingestIdentify` on every call, so apps that link the billing user id from several places (for example Keep Maps) could fire it three times per launch. 0.3.3 skips when the id matches the last successfully identified value (persisted like `trackInstall`), and coalesces concurrent identical calls into one request. A different id still posts.
- Add optional `codeMatches` and `offer.creatorName` on the typed `TapAppLinkRedeemResult` from `applyCode` (matches server redeem response fields).
- Send `X-TapAppLink-SDK-Version: 0.3.3` on all requests.

## 0.3.2

- **Fixed:** retrying `applyCode` after a timeout could count an install twice. 0.3.2 sends a request ID so the server recognises the retry. No code changes needed.
Expand Down
4 changes: 2 additions & 2 deletions PUBLISHING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ No repo secret is required for Option A. The release workflow already sets `perm
3. Tag that commit and push:

```bash
git tag v0.3.2
git push origin v0.3.2
git tag v0.3.3
git push origin v0.3.3
```

Do not retag an old commit with a new version name. The publish job checks that `v*` equals `package.json` version on the tagged commit.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ Works in Expo **dev clients and production builds**. Expo Go cannot load the And

## Install

Install **0.3.2 or later** of the SDK, together with AsyncStorage. AsyncStorage is required so install state survives cold launches; without it each launch can post a new install.
Install **0.3.3 or later** of the SDK, together with AsyncStorage. AsyncStorage is required so install state survives cold launches; without it each launch can post a new install.

**Expo**

```bash
npx expo install @tapapplink/react-native@0.3.2 @react-native-async-storage/async-storage
npx expo install @tapapplink/react-native@0.3.3 @react-native-async-storage/async-storage
```

**Bare React Native**

```bash
npm install @tapapplink/react-native@0.3.2 @react-native-async-storage/async-storage
# or: yarn add @tapapplink/react-native@0.3.2 @react-native-async-storage/async-storage
npm install @tapapplink/react-native@0.3.3 @react-native-async-storage/async-storage
# or: yarn add @tapapplink/react-native@0.3.3 @react-native-async-storage/async-storage
```

Bare React Native iOS apps should run `cd ios && pod install` after installing.
Expand Down Expand Up @@ -49,7 +49,7 @@ await TapAppLink.setAppUserId(await Purchases.getAppUserID());
const offer = TapAppLink.getOffer();
```

`trackInstall` posts `/ingestInstall` once per install (keyed by a persisted `installId`). Later launches restore the stored attribution id and offer without posting again. `setAppUserId` sends that stored `attributionId` after hydrate.
`trackInstall` posts `/ingestInstall` once per install (keyed by a persisted `installId`). Later launches restore the stored attribution id and offer without posting again. `setAppUserId` sends that stored `attributionId` after hydrate, posts `/ingestIdentify` only when the app user id changes, and coalesces concurrent identical calls.

Set `debug: true` to log each request, response and stored state. The API key is redacted in those logs.

Expand All @@ -59,7 +59,7 @@ Purchases are attributed with billing **webhooks**, not a client `trackPurchase`

Show success only when `applyCode` resolves. Map each result to UI state as below. Customers must never see the word "environment".

On a `network` (or timeout) error only, retry `applyCode` once with the same code. Do not auto-retry `unknownCode`, `inactiveCode`, or `wrongEnvironment`. If that retry also fails, show N6 ("We couldn't check your code. Check your connection and try again.") with a **Try again** button. From 0.3.2, that automatic retry is safe because the SDK reuses its request ID, so the server will not count the install twice.
On a `network` (or timeout) error only, retry `applyCode` once with the same code. Do not auto-retry `unknownCode`, `inactiveCode`, or `wrongEnvironment`. If that retry also fails, show N6 ("We couldn't check your code. Check your connection and try again.") with a **Try again** button. From 0.3.2, that automatic retry is safe because the SDK reuses its request ID, so the server will not count the install twice. From 0.3.3, success results may include optional `codeMatches` and `offer.creatorName`.

```ts
type RedeemUi = {
Expand Down
133 changes: 131 additions & 2 deletions __tests__/tapAppLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ describe("TapAppLink", () => {

const headers = (fetchMock.mock.calls[0][1] as { headers: Record<string, string> })
.headers;
expect(headers["X-TapAppLink-SDK-Version"]).toBe("0.3.2");
expect(SDK_VERSION).toBe("0.3.2");
expect(headers["X-TapAppLink-SDK-Version"]).toBe("0.3.3");
expect(SDK_VERSION).toBe("0.3.3");
});

it("tracks install once, sends installId, and caches offer", async () => {
Expand Down Expand Up @@ -182,6 +182,132 @@ describe("TapAppLink", () => {
});
});

it("skips ingestIdentify when appUserId matches the last successful identify", async () => {
const fetchMock = jest
.fn()
.mockResolvedValueOnce(jsonResponse(200, { attributionId: "attr_id" }))
.mockResolvedValueOnce(jsonResponse(200, { ok: true }));
global.fetch = fetchMock as unknown as typeof fetch;

configure();
await TapAppLink.trackInstall();
const first = await TapAppLink.setAppUserId("user_same");
const second = await TapAppLink.setAppUserId("user_same");

expect(first).toMatchObject({ ok: true });
expect(second).toMatchObject({
skipped: true,
appUserId: "user_same",
attributionId: "attr_id",
});
expect(fetchMock).toHaveBeenCalledTimes(2);
expect(asyncStore.get(STORAGE_KEY)).toContain("user_same");
});

it("does not post ingestIdentify again after a cold start with the same appUserId", async () => {
const fetchMock = jest
.fn()
.mockResolvedValueOnce(jsonResponse(200, { attributionId: "attr_cold_id" }))
.mockResolvedValueOnce(jsonResponse(200, { ok: true }));
global.fetch = fetchMock as unknown as typeof fetch;

configure();
await TapAppLink.trackInstall();
await TapAppLink.setAppUserId("user_persisted");
expect(fetchMock).toHaveBeenCalledTimes(2);

const persisted = asyncStore.get(STORAGE_KEY);
expect(persisted).toContain("user_persisted");
await TapAppLink.resetForTesting();
asyncStore.set(STORAGE_KEY, persisted!);

configure();
const resumed = await TapAppLink.setAppUserId("user_persisted");

expect(fetchMock).toHaveBeenCalledTimes(2);
expect(resumed).toMatchObject({
skipped: true,
appUserId: "user_persisted",
attributionId: "attr_cold_id",
});
expect(TapAppLink.getAppUserId()).toBe("user_persisted");
});

it("coalesces concurrent identical setAppUserId calls into one POST", async () => {
let resolveIdentify: ((value: unknown) => void) | undefined;
const identifyGate = new Promise((resolve) => {
resolveIdentify = resolve;
});
const fetchMock = jest.fn().mockImplementation(async (url: string) => {
if (String(url).includes("/ingestIdentify")) {
await identifyGate;
return jsonResponse(200, { ok: true });
}
return jsonResponse(200, { attributionId: "attr_race" });
});
global.fetch = fetchMock as unknown as typeof fetch;

configure();
await TapAppLink.trackInstall();

const a = TapAppLink.setAppUserId("user_race");
const b = TapAppLink.setAppUserId("user_race");
const c = TapAppLink.setAppUserId("user_race");
resolveIdentify?.(undefined);
const results = await Promise.all([a, b, c]);

expect(results[0]).toEqual(results[1]);
expect(results[1]).toEqual(results[2]);
expect(results[0]).toMatchObject({ ok: true });
const identifyCalls = fetchMock.mock.calls.filter((call) =>
String(call[0]).includes("/ingestIdentify"),
);
expect(identifyCalls).toHaveLength(1);
});

it("posts ingestIdentify again when appUserId changes", async () => {
const fetchMock = jest
.fn()
.mockResolvedValueOnce(jsonResponse(200, { attributionId: "attr_change" }))
.mockResolvedValueOnce(jsonResponse(200, { ok: true }))
.mockResolvedValueOnce(jsonResponse(200, { ok: true }));
global.fetch = fetchMock as unknown as typeof fetch;

configure();
await TapAppLink.trackInstall();
await TapAppLink.setAppUserId("user_a");
await TapAppLink.setAppUserId("user_b");

expect(fetchMock).toHaveBeenCalledTimes(3);
const secondIdentify = JSON.parse(
(fetchMock.mock.calls[2][1] as { body: string }).body,
) as { appUserId: string };
expect(secondIdentify.appUserId).toBe("user_b");
expect(TapAppLink.getAppUserId()).toBe("user_b");
expect(asyncStore.get(STORAGE_KEY)).toContain("user_b");
expect(asyncStore.get(STORAGE_KEY)).not.toContain("user_a");
});

it("does not skip identify after a failed ingestIdentify", async () => {
const fetchMock = jest
.fn()
.mockResolvedValueOnce(jsonResponse(200, { attributionId: "attr_fail" }))
.mockResolvedValueOnce(jsonResponse(500, { message: "identify failed" }))
.mockResolvedValueOnce(jsonResponse(200, { ok: true }));
global.fetch = fetchMock as unknown as typeof fetch;

configure();
await TapAppLink.trackInstall();
await expect(TapAppLink.setAppUserId("user_retry")).rejects.toThrow(
"identify failed",
);
const retry = await TapAppLink.setAppUserId("user_retry");

expect(retry).toMatchObject({ ok: true });
expect(fetchMock).toHaveBeenCalledTimes(3);
expect(asyncStore.get(STORAGE_KEY)).toContain("user_retry");
});

it("does not treat a non-2xx ingest response as success", async () => {
const fetchMock = jest.fn().mockResolvedValue(
jsonResponse(500, {
Expand Down Expand Up @@ -249,6 +375,7 @@ describe("TapAppLink", () => {
jsonResponse(200, {
attributionId: "attr_applied",
alreadyAttributed: false,
codeMatches: true,
offer: {
creatorName: "Ada",
promoCode: "ADA10",
Expand All @@ -263,6 +390,8 @@ describe("TapAppLink", () => {
expect(result).toMatchObject({
attributionId: "attr_applied",
alreadyAttributed: false,
codeMatches: true,
offer: { creatorName: "Ada" },
});
expect(TapAppLink.getOffer()?.creatorName).toBe("Ada");
expect(TapAppLink.getAttributionId()).toBe("attr_applied");
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tapapplink/react-native",
"version": "0.3.2",
"version": "0.3.3",
"description": "Tap App Link attribution SDK for React Native and Expo",
"license": "MIT",
"homepage": "https://tapapplink.com",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
type TapAppLinkEnvironment,
type TapAppLinkOffer,
type TapAppLinkRedeemErrorCode,
type TapAppLinkRedeemResult,
} from "./tapAppLink";
export type { PersistedInstallState } from "./storage";
export { TapAppLink as default } from "./tapAppLink";
4 changes: 4 additions & 0 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export type PersistedInstallState = {
installId: string;
tracked: boolean;
attributionId?: string;
/** Last app user id that successfully completed `/ingestIdentify`. */
appUserId?: string;
offer?: {
creatorName: string;
promoCode: string | null;
Expand Down Expand Up @@ -96,6 +98,8 @@ export const loadInstallState = async (
typeof parsed.attributionId === "string"
? parsed.attributionId
: undefined,
appUserId:
typeof parsed.appUserId === "string" ? parsed.appUserId : undefined,
offer: parsed.offer ?? null,
pendingRedeem,
};
Expand Down
Loading
Loading