Example 5: Origin audience routing
Scenario
A broadcaster serves a linear channel from a channel assembly origin that
uses program rules to deliver alternate content to different audiences. Viewers
in certain postal codes must receive a regional blackout feed. The player sends
the viewer's postal code as a zip query parameter. The broadcaster
wants MediaTailor to translate it into the origin's
aws.mediatailor.channel.audienceId query parameter on every
manifest request, without exposing the audience logic to the player.
Configuration
Map postal code to audience (CUSTOM_OUTPUT):
{ "FunctionId": "zipAudienceRouting", "FunctionType": "CUSTOM_OUTPUT", "CustomOutputConfiguration": { "Runtime": "JSONATA", "Output": { "manifest.aws.mediatailor.channel.audienceId": "{% player_params.zip in ['02101', '02102', '02108', '02110'] ? 'blackout' : 'allowed' %}" } } }
The output key uses the manifest. namespace, so MediaTailor removes
the prefix and appends the remainder,
aws.mediatailor.channel.audienceId, as a query parameter on
requests to the origin. The audience identifiers blackout and
allowed must match the audiences defined in the channel's
program rules. For more information, see Defining audience cohorts and alternate content with Program Rules.
Function mapping
{ "FunctionMapping": { "PRE_SESSION_INITIALIZATION": "zipAudienceRouting" } }
What happens when the function runs
A viewer starts a playback session with
?zip=02101on the session initialization request.MediaTailor runs the
PRE_SESSION_INITIALIZATIONlifecycle hook and runszipAudienceRouting. The function readsplayer_params.zip, finds it in the blackout list, and writesblackouttomanifest.aws.mediatailor.channel.audienceId.MediaTailor stores the value as a manifest query parameter on the session. Every request that MediaTailor makes to the origin for this session includes
?aws.mediatailor.channel.audienceId=blackout, and the origin returns the blackout feed.A viewer whose postal code is not in the list receives
audienceId=allowedand the regular feed. A viewer who sends nozipparameter also receivesallowed, becauseplayer_params.zipis undefined and theintest evaluates to false.
Tip
If the player also sends
manifest.aws.mediatailor.channel.audienceId as a query
parameter, the function's value takes precedence, so viewers cannot select
their own audience by editing the URL.
For more information, see Custom output, Pre-session initialization, and MediaTailor manifest query parameters.