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
9 changes: 4 additions & 5 deletions packages/audience/sdk-sample-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,17 @@ For dev-environment access, leave the key as a test key and set
SDK auto-derives sandbox vs prod from the key prefix; dev is not a
first-class environment and must be reached via explicit override.

## Ten-step walkthrough
## Nine-step walkthrough

1. Paste a test key into **Setup**, leave Initial Consent at `none`, click **Init**.
2. Open the **Consent** panel, click **anonymous**. Status bar updates.
3. Click **Lifecycle → page()**. A page message is queued.
4. Expand **Typed Events → purchase** (6th row in the accordion), fill in `currency=USD`, `value=9.99`, click **Send**. Watch the live TS snippet mirror the form as you type.
5. Set Consent to **full**.
6. In **Identity → Named identify**, enter `user@example.com`, type `email`, traits `{"name":"Jane"}`, click.
7. In **Identity → Traits-only identify**, enter `{"plan":"pro"}`, click.
8. In **Identity → Alias**, connect a Steam ID to the email above.
9. Set Consent back to **none**. Notice the queue purge in the event log.
10. In **Lifecycle → Simulate error**, pick `NETWORK_ERROR` from the dropdown and click **Fire onError**. The `onError` entry lands in the event log with the documented shape.
7. In **Identity → Alias**, connect a Steam ID to the email above.
8. Set Consent back to **none**. Notice the queue purge in the event log.
9. In **Lifecycle → Simulate error**, pick `NETWORK_ERROR` from the dropdown and click **Fire onError**. The `onError` entry lands in the event log with the documented shape.

## `AudienceEvents` catalogue

Expand Down
19 changes: 0 additions & 19 deletions packages/audience/sdk-sample-app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,25 +209,6 @@ <h1>
</div>
</details>

<details class="accordion-item">
<summary class="accordion-header">
<span class="accordion-title">Update traits</span>
</summary>
<div class="accordion-content">
<p class="helper-text">Update an existing player's traits without re-identifying.</p>
<div class="field">
<label for="traits-json">Traits (JSON)</label>
<textarea id="traits-json" rows="3" placeholder='{"plan":"pro","region":"AU"}'></textarea>
</div>
<div class="actions">
<button id="btn-identify-traits" disabled>
Update traits only
<small class="btn-sig">identify(traits)</small>
</button>
</div>
</div>
</details>

<details class="accordion-item">
<summary class="accordion-header">
<span class="accordion-title">Alias</span>
Expand Down
24 changes: 1 addition & 23 deletions packages/audience/sdk-sample-app/sample-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@
'btn-page', 'btn-flush', 'btn-reset', 'btn-shutdown',
'btn-consent-none', 'btn-consent-anon', 'btn-consent-full',
'btn-custom-event',
'btn-identify', 'btn-identify-traits',
'btn-identify',
];

function setInitState(on) {
Expand Down Expand Up @@ -559,7 +559,6 @@
$('btn-consent-anon').addEventListener('click', function () { onSetConsent('anonymous'); });
$('btn-consent-full').addEventListener('click', function () { onSetConsent('full'); });
$('btn-identify').addEventListener('click', onIdentify);
$('btn-identify-traits').addEventListener('click', onIdentifyTraits);
$('btn-alias').addEventListener('click', onAlias);
['alias-from-id', 'alias-to-id', 'alias-from-type', 'alias-to-type'].forEach(function (id) {
$(id).addEventListener('input', syncAliasButton);
Expand Down Expand Up @@ -822,27 +821,6 @@
}
}

function onIdentifyTraits() {
if (!audience) return;
if (!requiresFullConsent('identify()')) return;
var traits;
try { traits = parseTraits($('traits-json').value); }
catch (err) { log('identify()', 'invalid JSON: ' + err.message, 'err'); return; }
if (!traits) {
log('identify()', 'traits required', 'err');
return;
}
try {
audience.identify(traits);
enqueued();
identityMirror.traits = traits;
renderIdentityState();
log('identify(traits)', traits, 'ok');
} catch (err) {
log('identify(traits)', errMsg(err), 'err');
}
}

function onAlias() {
if (!audience) return;
if (!requiresFullConsent('alias()')) return;
Expand Down
47 changes: 0 additions & 47 deletions packages/audience/sdk/src/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,53 +628,6 @@ describe('Audience', () => {
expect(ids).toHaveLength(0);
sdk.shutdown();
});

it('ignores null passed as traits', async () => {
const sdk = createSDK({ consent: 'full' });

sdk.identify(null as any);
await sdk.flush();

const ids = sentMessages().filter((m: any) => m.type === 'identify');
// null is not a valid traits object — should not enqueue
expect(ids).toHaveLength(0);

sdk.shutdown();
});

it('ignores array passed as traits', async () => {
const sdk = createSDK({ consent: 'full' });

sdk.identify(['not', 'traits'] as any);
await sdk.flush();

const ids = sentMessages().filter((m: any) => m.type === 'identify');
expect(ids).toHaveLength(0);

sdk.shutdown();
});

it('sends anonymous identify with traits only', async () => {
const sdk = createSDK({ consent: 'full' });

sdk.identify({
source: 'steam',
steamId: TEST_STEAM.id,
});
await sdk.flush();

const msg = sentMessages().find(
(m: any) => m.type === 'identify',
);
expect(msg).toBeDefined();
expect(msg.userId).toBeUndefined();
expect(msg.traits).toEqual({
source: 'steam',
steamId: TEST_STEAM.id,
});

sdk.shutdown();
});
});

describe('alias', () => {
Expand Down
30 changes: 5 additions & 25 deletions packages/audience/sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,43 +285,23 @@ export class Audience {
* an account. Before identify(), the SDK only knows an anonymous cookie ID.
* After, all future events are tied to this player.
*
* Named: `sdk.identify('user@example.com', 'email', { name: 'Jane' })`
* Traits only: `sdk.identify({ source: 'steam', steamId: '765...' })`
* `sdk.identify('user@example.com', 'email', { name: 'Jane' })`
*
* Requires 'full' consent.
*/
identify(id: string, identityType: IdentityType, traits?: UserTraits): void;

identify(traits: UserTraits): void;

identify(
idOrTraits: string | UserTraits,
identityType?: IdentityType,
traits?: UserTraits,
): void {
identify(id: string, identityType: IdentityType, traits?: UserTraits): void {
if (!canIdentify(this.consent.level)) {
this.debug.logWarning('identify() requires full consent — call ignored.');
return;
}
getOrCreateSession(this.cookieDomain);

if (idOrTraits !== null && typeof idOrTraits === 'object' && !Array.isArray(idOrTraits)) {
this.enqueue('identify', {
...this.baseMessage(),
type: 'identify',
traits: idOrTraits,
});
return;
}

if (typeof idOrTraits !== 'string') return;

const id = truncate(idOrTraits);
this.userId = id;
const resolvedId = truncate(id);
this.userId = resolvedId;
this.enqueue('identify', {
...this.baseMessage(),
type: 'identify',
userId: id,
userId: resolvedId,
identityType,
traits,
});
Expand Down
Loading