Playback telemetry

By default, the SDK marks a voice recording, an audio file or a video played as soon as it starts playing, and the author sees "Played". This UI cookbook replaces that with a handler of your own: it waits until the user has played a share of the media that depends on its type, then reports the play to your analytics and marks the message. Marks themselves are explained in Message marks.

Note:

What you'll build

  • A different share of the media for voice recordings, audio files and videos before a play counts
  • A telemetry event for each play that counts, with the attachment type, the message and how far the user got
  • The played mark sent at the same moment, so the author's label means the same as your analytics
  • The handler set once, for the message list and the image gallery alike

Approach: Compose. Choosing where to start explains the approaches.

Best practices

  • Decide in handlePlaybackStarted on ChatConfigProvider, not in a replaced AudioAttachment. The handler runs for the SDK's audio players and for the image gallery's videos, so one rule covers both.
  • Return false until the play counts. The player asks again on its next progress report, and stops asking once you return true.
  • Compare player.position with player.duration, both in milliseconds. A player's duration is 0 until its media loads, so wait while it is.
  • Call markPlayed() in the same branch that reports the play, so the author's "Played" and your analytics count the same plays.
  • Put ChatConfigProvider above OverlayProvider, which renders the gallery, and give it a value declared at module scope.
  • Count unique plays in your analytics by message and attachment. A new player asks again, for example when the user opens the gallery a second time.

The handler

Each attachment type gets the share of its length that the user has to play: most of a voice recording, half of an audio file, a quarter of a video. The handler reads the type from the attachment, and the position and the length from the player:

src/playbackTelemetry.ts
import type { HandlePlaybackStarted } from "stream-chat-react-native";

// The share of its length a user plays before the play counts.
const PLAYED_SHARE: Record<string, number> = {
  audio: 0.5,
  video: 0.25,
  voiceRecording: 0.8,
};

type PlaybackEvent = {
  attachmentType: string;
  channelId?: string;
  durationMs: number;
  messageId: string;
  positionMs: number;
};

// Replace with your analytics client.
const track = (name: "media_played", event: PlaybackEvent) => {
  console.log(name, event);
};

export const handlePlaybackStarted: HandlePlaybackStarted = ({
  attachment,
  markPlayed,
  message,
  player,
}) => {
  const attachmentType = attachment.type ?? "unknown";
  const { duration, position } = player;
  const share = PLAYED_SHARE[attachmentType] ?? 0;

  // Not loaded yet, or not far enough: asked again on the next progress report.
  if (duration === 0 || position < duration * share) {
    return false;
  }

  track("media_played", {
    attachmentType,
    channelId: message.cid,
    durationMs: duration,
    messageId: message.id,
    positionMs: position,
  });
  markPlayed();
  return true;
};

An attachment type the table doesn't list counts on its first progress report, as the SDK does by default. To report a type without marking it, return true without calling markPlayed().

Set it once

ChatConfigProvider takes the handler. Wrap OverlayProvider in it, so the image gallery, which OverlayProvider renders, uses it too:

App.tsx
import type { StreamChat } from "stream-chat";
import {
  Chat,
  ChatConfigProvider,
  OverlayProvider,
} from "stream-chat-react-native";

import { handlePlaybackStarted } from "./src/playbackTelemetry";

// Declared once at module scope, so the value keeps its identity.
const chatConfig = { handlePlaybackStarted };

export const App = ({ client }: { client: StreamChat }) => (
  <ChatConfigProvider value={chatConfig}>
    <OverlayProvider>
      <Chat client={client}>{/* your navigation */}</Chat>
    </OverlayProvider>
  </ChatConfigProvider>
);

The value replaces the provider's default as a whole. If you set resizableCDNHosts, put it in the same object.

The recording below was played past 80% of its length, so it reads "Played". The video was not played, so it has no label.

Played under a voice recording played past its share, and no label under an unplayed video, on iOS
Played under a voice recording played past its share, and no label under an unplayed video, on Android

How it works

Info:

Example

Cleo sends Dan a 20-second voice recording and a 2-minute video. Dan presses play on the recording:

  1. The recording downloads. While it does, its player reports no progress, so the handler isn't called.
  2. Playback starts. On each progress report, the player calls the handler with the recording's attachment and the player. Its duration is 20,000 milliseconds and the share for voiceRecording is 0.8, so the handler returns false until the position passes 16,000.
  3. At 16 seconds the handler reports media_played with attachmentType: "voiceRecording" and positionMs just past 16,000, calls markPlayed() and returns true. The player stops asking.
  4. Cleo's copy of the message gets the mark from the message.mark_added event, and its footer reads "Played".
  5. Dan opens the video in the gallery and closes it after 20 seconds. The share for video is 0.25, 30 seconds of 2 minutes, so the video is neither reported nor marked.
  • The player asks only while it plays, past the start. A paused player reports nothing. A seek reports the new position, so a user who drags to the end of a recording passes the threshold.
  • The position is the media's own time. At x2.0 speed, 16 seconds of a recording play in 8.
  • The SDK's audio players live as long as the channel screen, and the gallery's players until the gallery closes. A new player asks again, so a recording or a video that was played before can be reported again. The mark isn't sent twice: markMessagePlayed skips a message the user already marked, and the server ignores a repeat.
  • A preview in the composer never reaches the handler. The user's own messages and channels whose type has no played mark do, and markPlayed() sends nothing for them. To leave the user's own plays out of your analytics, compare message.user?.id with the connected user's ID.

Read more