コンテンツにスキップ

Activities

Part of the Ads use case.

Record a human score for any bounded activity a user does in your app: a game round, a quiz, a lesson, a video. You use the ad module with an activity id in place of the ad unit. You open a record when the activity starts and end it when the activity ends. DEA keeps one record for each activity, with the human score at the start and the human score at the end.

Term Meaning
Activity One bounded thing a user does, from its start to its end: one game round, one quiz, one lesson, one video
Activity id The text that names a kind of activity, such as quiz_weekly. You choose it and pass it to the SDK
Record What DEA keeps for one activity: the activity id, the human score at the start, the human score at the end, and the time

When to use it

  • You want a human score for each game round, quiz, lesson or video
  • You want to see how many of your activities real people complete

Prerequisites

  • The SDK is installed and initialized with your appId and appKey. See the Quick Start.
  • Your app calls attest() once when it starts, and gets 'passed'.
  • The upload helper of the Ads page, from its section "Sign and upload one event", is in your app. The samples below use it.
  • DEA has registered your activity ids. See Activity ids.

Activity ids

Rule Example
Letters, digits, - and _ only quiz_weekly
50 characters at most game_puzzle_daily
One id for each kind of activity, unique in your app. Do not change it after the first record lesson_intro

DEA registers each activity id as an ad of your app, so that its records appear under your ads. Send your DEA contact one row for each activity id:

Field Rule Example
Activity id The rules above quiz_weekly
Name The name people read in reports, 255 characters at most Weekly Quiz

DEA keeps a record whose activity id it has not registered, but does not list it under your ads.

Usage

Record one activity

Open the record when the activity starts. End it when the activity ends. At each step, signAndUpload signs the score with getProof() and sends it. The comments give the order of the calls.

import * as Dea from '@dea/react-native';
import {signAndUpload} from './dea-upload';

/** The record of one activity. Create one for each activity. */
export class ActivityRecord {
  private recordId: string | null = null;
  private queue: Promise<unknown> = Promise.resolve(); // the start goes up before the end

  constructor(
    private readonly activityId: string,
    private readonly userId?: string, // your app user id
  ) {}

  /** Call when the activity starts. */
  started(): void {
    this.queued(async () => {
      // 1. Too early to score: a start call now makes no record.
      if ((await Dea.getClassification()).result === 'insufficient') return;

      // 2. The device key is registered. Answers at once while the last registration is valid.
      if ((await Dea.attest()).verdict !== 'passed') return;

      // 3. Sign the score at the start and send it: POST /api/sessions.
      //    The session id is empty: DEA makes the record id.
      const answer = await signAndUpload('ad.impression', '', this.activityId, {}, this.userId);
      if (answer.kind !== 'accepted') return; // not recorded: read answer

      // 4. Keep the record id from the answer.
      const id = answer.body?.session_id;
      if (typeof id === 'string' && id !== '') this.recordId = id;
    });
  }

  /** Call when the same activity ends. */
  ended(): void {
    this.queued(async () => {
      const id = this.recordId;
      if (id === null) return; // the start was not recorded
      this.recordId = null;

      // 5. Sign the score at the end with the record id, and send it: PUT /api/sessions/{id}.
      await signAndUpload('ad.ended', id, this.activityId, {}, this.userId);
    });
  }

  private queued(step: () => Promise<void>): void {
    this.queue = this.queue.then(step).catch(() => {});
  }
}

signAndUpload is the helper of the Ads page, with the appScopedUid argument its section "App user id" describes.

Call it from your own code:

const record = new ActivityRecord('quiz_weekly', userId);
record.started(); // when the activity starts
// ...
record.ended(); // when the same activity ends

Pass the app user id

userId is your app's id for the user. The class passes it to signAndUpload, which signs it as appScopedUid into both proofs, so DEA stores it on the record. Leave it out and no id is sent. See App user ids.

Check the result

Cause What you read What to do
The session was not sufficient when the activity started getProof() resolves null, and getLastProofRefusal() resolves not_scored Nothing. The user had made too few actions before this activity
No device key, or the SDK core is not running getLastProofRefusal() resolves no_key or no_core See Troubleshooting
A production appId without appKey getLastProofRefusal() resolves app_key_missing Pass your appKey to the SDK
DEA refused the proof, or did not answer The upload answers a 4xx, or no answer after five sends Check that attest() passes, and that appKey is the key issued with your appId
DEA accepted the start but answered no record id The answer has no session_id Tell your DEA contact

Notes

Note Why
An activity that starts before the session is sufficient has no record The SDK signs no score before the session is sufficient, and does not keep the start to sign later. This is most often the first activity after the app starts. A missing record does not mean that the user is a bot
Read getClassification().result before the start call. When it is 'insufficient', make the start call later, when it is not A start call made too early makes no record, and the SDK does not try it again
End the record while the app is open An end sent while the app closes can be cancelled. The record then has no score at the end
End a record before you open the next one The SDK follows one open record at a time
Do not wait for the start call before you start the activity When the network fails, the start call can take up to 90 seconds
Call attest() once when the app starts It answers at once while the last registration is valid, so the start of an activity does not wait for it
For an activity of a few seconds, record one whole level or one match, not each round Each record costs two signed proofs and two requests

API

Member Returns Description
getProof(event, options?) Promise<SignedProof \| null> Signs the score. 'ad.impression' with sessionId: '' for the start, 'ad.ended' with the record id for the end. options: sessionId, adUnit, appScopedUid
getLastProofRefusal() Promise<string \| null> Why the last getProof() resolved null
getClassification() Promise<Classification> Read result before the start call
attest() Promise<Attestation> Registers the device key. See Attest