Set Storyteller.sharedInstance.delegate to handle events from every
Storyteller view and player: analytics events, share taps, ad requests, and
in-app action links. The delegate is an object typed IStorytellerDelegate.
For loading and dismissal events from one row, grid, or Clips player, see
Handle view callbacks.
Assign an object with the callbacks you need to
Storyteller.sharedInstance.delegate. Set it before you call initialize, so
that it receives the sdkInitialized event.
Assigning delegate replaces all four global callbacks. A callback you leave out of the new object stops being called, even if an earlier assignment set it. Define every callback in one object, or spread the current delegate into the new one.
constgetAdConfig=(adRequestInfo)=>{// Return { slot, customTargeting, publisherProvidedId }, or null for no ad.// See Integrate ads.returnnull;};Storyteller.sharedInstance.delegate={...Storyteller.sharedInstance.delegate,getAdConfig,};
Called when an analytics event occurs in a Story or Clips player. A view with a
context returns that value in
data.context. See Analytics context for the supported
views, inheritance rules, and data boundary. The SDK calls this callback only
while enableUserActivityTracking is on, and sends ad events only while
enableAdTracking is also on. See Integrate analytics for the
event types.
Called when the user taps the share button in a Story or a Clip. Use it to
replace the default share behavior. The SDK passes the text, title, and URL it
would otherwise share, and pauses the Story or Clip. When your promise
resolves, the SDK records a shareSuccess event. When the promise resolves or
rejects, the Story or Clip plays again.
If you don't implement this callback, the SDK opens the browser's share sheet
with navigator.share. The share button appears only in browsers that support
navigator.share, even when you implement this callback. Story Pages that
share their media file download it instead and don't call this callback.
Called when a Story or Clips player needs an ad and your tenant uses Google Ad
Manager ads. Return an object with these fields, or null for no ad:
slot: the ad unit path to request
customTargeting (optional): key-value pairs for ad targeting
publisherProvidedId (optional): your publisher provided ID
Story ad requests include story. Clips ad requests include clip,
nextClip, and collection, and have no story field. The SDK doesn't
export the AdConfig type. See Integrate ads for the full request
and response details.
The Storyteller Web Showcase's
buildAdConfig
shows how to return slots and custom targeting values.
Called when a user taps an action button in a Story or Clip that links into
your app (an inApp action). Route the user to url in your app. If you don't
implement this callback, inApp actions open like regular URLs.
For a full implementation that wires every callback, see the Storyteller Web
Showcase's
attachStorytellerDelegate.
{"slug": "storyteller-delegate", "page_title": "Handle Global Callbacks", "page_url": "StorytellerDelegate/", "canonical_url": "/web/StorytellerDelegate/", "markdown": "# Handle global callbacks {#implementing-storytellerdelegate-callbacks}\n\nSet `Storyteller.sharedInstance.delegate` to handle events from every\nStoryteller view and player: analytics events, share taps, ad requests, and\nin-app action links. The delegate is an object typed `IStorytellerDelegate`.\nFor loading and dismissal events from one row, grid, or Clips player, see\n[Handle view callbacks](StorytellerListViewDelegate.md).\n\n## Set the delegate {#how-to-use}\n\nAssign an object with the callbacks you need to\n`Storyteller.sharedInstance.delegate`. Set it before you call `initialize`, so\nthat it receives the `sdkInitialized` event.\n\n```javascript\nStoryteller.sharedInstance.delegate = {\n onUserActivityOccurred: (type, data) => {\n console.log(type, data.context);\n },\n};\n```\n\n!!! warning\n\n Assigning `delegate` replaces all four global callbacks. A callback you leave out of the new object stops being called, even if an earlier assignment set it. Define every callback in one object, or spread the current delegate into the new one.\n\n```javascript\nconst getAdConfig = (adRequestInfo) => {\n // Return { slot, customTargeting, publisherProvidedId }, or null for no ad.\n // See Integrate ads.\n return null;\n};\n\nStoryteller.sharedInstance.delegate = {\n ...Storyteller.sharedInstance.delegate,\n getAdConfig,\n};\n```\n\n## Callbacks {#methods}\n\nAll callbacks are optional.\n\n### onUserActivityOccurred\n\n```typescript\nonUserActivityOccurred?: (type: ActivityType, data: UserActivityData) => void;\n```\n\nCalled when an analytics event occurs in a Story or Clips player. A view with a\n[`context`](StorytellerListView.md#context) returns that value in\n`data.context`. See [Analytics context](Analytics.md#context) for the supported\nviews, inheritance rules, and data boundary. The SDK calls this callback only\nwhile `enableUserActivityTracking` is on, and sends ad events only while\n`enableAdTracking` is also on. See [Integrate analytics](Analytics.md) for the\nevent types.\n\nThe Storyteller Web Showcase has an\n[`onUserActivityOccurred` handler](https://github.com/getstoryteller/storyteller-showcase-web/blob/11.0.0/nextjs/src/components/contexts/storytellerSdkDelegate.ts#L60).\n\n### onShareButtonTapped\n\n```typescript\nonShareButtonTapped?: (text: string, title: string, url: string) => Promise<void>;\n```\n\nCalled when the user taps the share button in a Story or a Clip. Use it to\nreplace the default share behavior. The SDK passes the text, title, and URL it\nwould otherwise share, and pauses the Story or Clip. When your promise\nresolves, the SDK records a `shareSuccess` event. When the promise resolves or\nrejects, the Story or Clip plays again.\n\nIf you don't implement this callback, the SDK opens the browser's share sheet\nwith `navigator.share`. The share button appears only in browsers that support\n`navigator.share`, even when you implement this callback. Story Pages that\nshare their media file download it instead and don't call this callback.\n\nThe Storyteller Web Showcase shows an\n[`onShareButtonTapped` override](https://github.com/getstoryteller/storyteller-showcase-web/blob/11.0.0/nextjs/src/components/contexts/storytellerSdkDelegate.ts#L65).\n\n### getAdConfig\n\n```typescript\ngetAdConfig?: (adRequestInfo: StorytellerAdRequestInfo) => AdConfig | null;\n```\n\nCalled when a Story or Clips player needs an ad and your tenant uses Google Ad\nManager ads. Return an object with these fields, or `null` for no ad:\n\n- `slot`: the ad unit path to request\n- `customTargeting` (optional): key-value pairs for ad targeting\n- `publisherProvidedId` (optional): your publisher provided ID\n\nStory ad requests include `story`. Clips ad requests include `clip`,\n`nextClip`, and `collection`, and have no `story` field. The SDK doesn't\nexport the `AdConfig` type. See [Integrate ads](Ads.md) for the full request\nand response details.\n\nThe Storyteller Web Showcase's\n[`buildAdConfig`](https://github.com/getstoryteller/storyteller-showcase-web/blob/11.0.0/nextjs/src/components/contexts/storytellerSdkDelegate.ts#L5)\nshows how to return slots and custom targeting values.\n\n### userNavigatedToApp\n\n```typescript\nuserNavigatedToApp?: (url: string) => void;\n```\n\nCalled when a user taps an action button in a Story or Clip that links into\nyour app (an `inApp` action). Route the user to `url` in your app. If you don't\nimplement this callback, `inApp` actions open like regular URLs.\n\n## Delegate interface\n\n```typescript\ninterface IStorytellerDelegate {\n onUserActivityOccurred?: (type: ActivityType, data: UserActivityData) => void;\n\n onShareButtonTapped?: (\n text: string,\n title: string,\n url: string\n ) => Promise<void>;\n\n getAdConfig?: (adRequestInfo: StorytellerAdRequestInfo) => AdConfig | null;\n\n userNavigatedToApp?: (url: string) => void;\n}\n```\n\nFor a full implementation that wires every callback, see the Storyteller Web\nShowcase's\n[`attachStorytellerDelegate`](https://github.com/getstoryteller/storyteller-showcase-web/blob/11.0.0/nextjs/src/components/contexts/storytellerSdkDelegate.ts#L55).\n", "copy_markdown_include_header": false, "base_path": "", "ai_dir": "ai", "missing_payload_behavior": "empty"}