AWS for M&E Blog

Personalizing sports content with CloudFront Functions

Introduction

Each day, we are presented with information that is personalized to us. This comes in the form of advertisements, social media content, entertainment, and more. This personalization helps us to meet consumer demand and influence consumer behavior. From the perspective of sports franchises, there is yet unmet demand to deliver personalized content to both avid and passive fans. One potential use case is to deliver targeted content based on a consumer’s geographic location using a suite of services from Amazon Web Services (AWS). When combined with advertising, this targeted content not only meets customer demand but may also help drive other revenue streams.

Amazon CloudFront—a content distribution network (CDN) service built for high performance, security, and developer convenience—contains a feature called geolocation headers that facilitates content delivery based on location. These geolocation headers include country, region or state, city, postal code, time zone, and metro code, allowing the personalization to be very granular. Amazon CloudFront also contains a feature called CloudFront Functions so that you can write lightweight functions in JavaScript for high-scale, latency-sensitive CDN customizations. Using the data from geolocation headers with CloudFront Functions, we can provide highly personalized content to consumers. We can also add additional capabilities when we look at AWS Lambda@Edge, which lets you run code closer to users of your application; Amazon Personalize, which allows developers to quickly build and deploy curated recommendations and intelligent user segmentation at scale using machine learning (ML); and Amazon S3 Object Lambdas (S3 Object Lambdas), where you can add your own code to modify and process data as it is returned to an application, to enhance the solution.

Solution overview

Approach 1 – Using CloudFront Functions to assign paths based on Amazon CloudFront geolocation headers

Architecture using CloudFront Functions

Figure 1. Architecture using CloudFront Functions

In the first approach, we personalize content for users based on their geographic information provided by CloudFront Functions. CloudFront Functions uses these headers to determine which image should be returned to the user by using path manipulation. For example, when an anonymous user navigates to a webpage, CloudFront Functions can determine the user’s location and change the path in Amazon Simple Storage Service (Amazon S3), an object storage service, for that particular location and return these images to the user.

An example of how this approach can be used is to envision a sports league providing customized content for anonymous users based on their locations. In this scenario, we can customize the web application to display content for a home team for the anonymous user. The home team for the user is determined based on a simple algorithm of location, distance, and popularity of the teams in the user’s region. Amazon CloudFront uses path manipulation to redirect the user request to the correct images for their home team to be displayed. Now, when an anonymous user visits this sports league’s web application, it will display the viewer’s home team logo and all relevant images using path manipulation without first requiring the user to sign in to the application.

Approach 2 – Using Lambda@Edge to get Amazon Personalize object recommendations for a user

Figure 2. Architecture using AWS Lambda@Edge and Amazon Personalize

Figure 2. Architecture using AWS Lambda@Edge and Amazon Personalize

The second approach replaces CloudFront Functions with Lambda@Edge and allows us to add more logic to the process by using Amazon Personalize to get more targeted personalization recommendations for an anonymous user.

We rely on the geolocation headers that Amazon CloudFront makes available. We might also want to examine other Amazon CloudFront headers, such as the viewer’s device type. This header information is used by our Lambda@Edge function as it seeks an inference from Amazon Personalize. This recommendation is used to determine the best content to return to our user and rewrites the URL accordingly.

In this approach, Lambda@Edge essentially provides the same functionality as the CloudFront Functions in our first approach. Because CloudFront Functions do not support integration with other AWS services, we must use Lambda@Edge, which is an extension of AWS Lambda—a serverless, event-driven compute service—so we have the option to code in Node.js or Python. It is important to note that even though Lambda@Edge will initiate the functions in AWS locations that are closer to the viewer than standard AWS Lambda functions, there is additional latency when compared with CloudFront Functions, and this must be taken into consideration for a given use case.

The benefit of Amazon Personalize in this approach is that we do not have to statically code the logic for our geolocation-based personalization because the Amazon Personalize recommendation model is trained by a dataset provided by us for our specific purpose. Obtaining an appropriate dataset to train a recommendation model is no small task, but the result can provide better personalization.

To understand how this all comes together, let’s revisit our goal from our first approach. We want to take a generic sports website element and respond with appropriate home-team content based on geolocation data. When we expand that to use Amazon Personalize, we might find that a fan in a given zip code who uses an iOS device is more likely to have an affinity for one team, and a fan in that same zip code who uses an Android device prefers a different team. That level of insight is difficult to glean without Amazon Personalize. But knowing that inference, we can provide better content to our anonymous users.

Learn more about Lambda@Edge and Amazon Personalize in these Developer Guides:

Lambda@Edge
https://docs.thinkwithwp.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-the-edge.html

Amazon Personalize
https://docs.thinkwithwp.com/personalize/latest/dg/what-is-personalize.html

Approach 3 – S3 Object Lambdas with Amazon Personalize

Architecture using CloudFront, S3 Object Lambdas, and Amazon Personalize

Figure 3. Architecture using Amazon CloudFront, S3 Object Lambdas, and Amazon Personalize

A third approach to personalizing sports content for anonymous users based on geographic information uses S3 Object Lambdas.

S3 Object Lambdas allow us to modify and process data as it is returned from Amazon S3 by using custom logic that runs within an AWS Lambda function. In our example architecture, the

AWS Lambda function calls out to Amazon Personalize, passes the incoming request headers from Amazon CloudFront, and uses the personalization inference returned by Amazon Personalize to manipulate the original S3 Object Lambda and returns the result.

This approach allows for dynamic customization with greater flexibility than the first two methods. The previous methods each require pre-generated media content and routing between them. Using S3 Object Lambdas facilitates almost infinite variety because the object’s customization is set dynamically upon request of the object.

Because the object is being manipulated upon request, it is important to factor in the AWS Lambda latency when deciding whether this approach is a fit for a given use case. Latency in altering media objects will likely disqualify this approach for near real-time use cases where video content needs to be personalized, but it can be well suited for text manipulation and simple image manipulation.

An example of how this approach can be used is to customize a player-highlight element on a webpage for a given viewer. Having already created match-relevant content for every player in a league could be a limiting factor for implementing approaches 1 or 2, from both an Amazon S3 storage perspective as well as a process for automating the generation for each player. With S3 Object Lambdas, a sports league or a third-party site, like a fantasy sports provider, can dynamically customize content with players that Amazon Personalize infers as relevant to their users.

Considerations

S3 Object Lambdas allow content to be manipulated, providing deeper personalization than matching a user to a given preexisting object. But the greater response latency of this approach means that, in near real-time user applications, it is better suited for text objects, and approaches 1 and 2 are more performant for media content, like images and videos.

Conclusion

In this blog post, we described three different approaches for customizing the content delivered to your users. We covered how geolocation headers from Amazon CloudFront can be used to add data around your incoming requests and reviewed CloudFront Functions, Lambda@Edge, and S3 Object Lambdas as options for performing personalization on the content that you serve to your customers.

To get hands on with CloudFront Functions and Lambda@Edge, try the Handling Rewrites and Redirects using Edge Functions workshop.

Further reading

Leverage Amazon CloudFront geolocation headers for state-level geo-targeting

Use Your Code to Process Data as It Is Being Retrieved from S3

Patrick Gryczka

Patrick Gryczka

Patrick Gryczka is a Sr. Solutions Architect with the AWS Sports SA team. His core areas of focus are Serverless and Web3 technologies. Prior to life as a Solutions Architect, Patrick worked as a consultant for ecommerce and fintech customers adopting cloud technologies. Patrick is based out of New York City and lives in Brooklyn with his wife and three cats.

Karl Sung

Karl Sung

Karl Sung is a Solutions Architect working with enterprise clients based out of New York City. He has been in the IT field for over 20 years with a background focused on infrastructure and containers.

Mike Lally

Mike Lally

Mike is a Senior Solutions Architect, Sports at AWS, focused on helping his customers achieve great outcomes. He loves being a nerd dad, running and triathlon.