Logo

dev-resources.site

for different kinds of informations.

Click Click… Configuring Custom Domain SES Tracking with AWS CDK

Published at
2/22/2024
Categories
aws
cdk
ses
tutorial
Author
niallkeys
Categories
4 categories in total
aws
open
cdk
open
ses
open
tutorial
open
Author
9 person written this
niallkeys
open
Click Click… Configuring Custom Domain SES Tracking with AWS CDK

What is SES Click Tracking?

In this blog I'm assuming prior knowledge of AWS SES (Send Email Service) but as the name suggests it's basically the service that allows us to send emails in AWS. However, it has a number of other great features that takes it beyond this simple explanation.

One such feature this service offers is click tracking. When this is enabled AWS intercepts any links in an email with a domain hosted by SES before then redirecting to the intended destination as shown in the diagram. This allows you to get click events and metrics back to use in your system.

Image description

Challenges with default URLs

By default AWS use a domain hosted by them to make this process as simple as a click of a button in the console.

However, this URL is blocked by most ad blockers which leaves customers unable to navigate to any links included in emails that are sent via SES.

This was exactly our experience on a recent project causing our customers to not be able to follow links to our webapp.

This meant we needed to step away from the default setup and for this we must look to custom url tracking.

If you are here it's likely that your experience is the same but with the following steps, your users will be able to click with ease in no time!

1. Decide on your sub domain

The first thing you must consider as part of this is what subdomain you'd like to use for your click tracking.

In this scenario I'm going to use click.mydomain.com. Next, you'll need to add it as a verified identity to SES. However, if the subdomain you want to use parent's domain is already a verified identity in SES as it was for us, you can skip this step!

2. Get it certified!

Once your subdomain is ready, the next thing you need to do is get a SSL certificate for HTTPS. For this we used ACM (Amazon Certificate Manager), however you can get this from any trusted Certificate Authority.

It's important to note here that because we want to use this certificate in a CloudFront distribution (spoiler) we must request it from us-east-1.

const certificate = new Certificate(this, "TrackingDomainCertificate", {
  domainName: "click.mydomain.com",
  validation: CertificateValidation.fromDns(this.props.hostedZone)
});

Enter fullscreen mode Exit fullscreen mode

3. A CloudFront Distribution

The next thing we need to define is a new CDN, as already given away, for this we look to CloudFront.

In this we must define three things the domains used, the behaviour we must apply and the certificate we just discussed above.

The behaviour we defined is what allows us to configure CloudFront to the origin following the pattern r.<region>.awstrack.me, where region is wherever we are sending the SES emails from.

Additionally as part of the behaviour in CloudFront we must include a cache policy as we need to past on the Host header from the initial request to SES so that it can track events from the URL.

const engagementDistribution = new Distribution(this, "EngagementDistribution", {
  defaultBehavior: {
    origin: new HttpOrigin("r.eu-west-2.awstrack.me"),
    cachePolicy: new CachePolicy(this, "TrackingCachePolicy", {
      cachePolicyName: "TrackingCachePolicy",
      comment: "Policy to cache host header",
      headerBehavior: {
        behavior: "whitelist",
        headers: ["Host"]
      }
    })
  },
  domainNames: ["click.mydomain.com"],
  certificate: this.certificate
});
Enter fullscreen mode Exit fullscreen mode

4. Your DNS tool of choice

As we used AWS Route 53 combined with using CloudFront as our CDN this couldn't be any simpler as we just have to create an alias record shown below to point the domain at the CloudFront distribution we've just defined.

const trackingDomainNameRecord = new ARecord(this, "TrackingDomainRecord", {
  recordName: this.deliveryTrackingDomainName,
  zone: this.props.hostedZone,
  target: RecordTarget.fromAlias(new CloudFrontTarget(this.engagementDistribution))
});
Enter fullscreen mode Exit fullscreen mode

5. Update your SES config!

It's worth noting that the domain record can take up to 24 hours to become available, but once this is ready you are able to switch your configuration set to use this new domain.

new ConfigurationSet(this, "ExampleConfigurationSet", {
  configurationSetName: "ExampleConfigurationSet",
  trackingOptions: {
    customRedirectDomain: "click.mydomain.com"
  }
});

Enter fullscreen mode Exit fullscreen mode

Now once all the above is deployed we simply need to specify the ConfigurationSetName attribute when sending an email using SES. For example:

await sesClient.send(new SendTemplatedEmailCommand({
  Source: "[email protected]",
  Destination: {
    ToAddresses: "[email protected]",
  },
  Template: "templateName",
  TemplateData: JSON.stringify(templateProps),
  ConfigurationSetName: "ExampleConfigurationSet"
}));
Enter fullscreen mode Exit fullscreen mode

Following all these steps will allow your users to click through with ease once again!

ses Article's
30 articles in total
Favicon
Amazon SES Unwrapped: Key Lessons & Testing Tips for Building Robust Email Systems
Favicon
Setting Up and Handling Email Aliases in AWS SES
Favicon
AWS workshop #2: Leveraging Amazon Bedrock to enhance customer service with AI-powered Automated Email Response
Favicon
Sending Emails with Spring Boot, AWS SES, and Serverless Lambda for Scalable Solutions
Favicon
Building smarter RSS feeds for my newsletter subscriptions with SES and Bedrock
Favicon
Unleashing the Power of Python Lambda Functions with Terraform for Email Automation via AWS SES
Favicon
Differences Between Amazon SES, Amazon SNS, and Amazon Pinpoint
Favicon
Getting production access to AWS SES (2024)
Favicon
Implementing a Mail Delivery Switch in Python for Local and AWS Environments Using Amazon SES
Favicon
Security Protocol to connect AWS SES
Favicon
Trigger a Typescript AWS Lambda on Receiving an Email with SES
Favicon
Click Click… Configuring Custom Domain SES Tracking with AWS CDK
Favicon
How to setup SES Email templates [2024 Guide]
Favicon
Why I Built the SES Easy Mailer Node Module
Favicon
Configurando Amazon SES para envio de e-mail
Favicon
Monitoramento de Eventos no AWS SES com Filtros no CloudWatch Logs
Favicon
Mail-in-a-Box (Relay AWS SES)
Favicon
Streamline Email Sending with AWS SES, Lambda, and S3 Integration
Favicon
AWS Lambda with CloudWatch for Seamless EC2 State Change Notifications through SES.
Favicon
Send mail by SES CLI
Favicon
CLIでメールを送信する
Favicon
Sending Email with Amazon SES on NodeJS
Favicon
Integrating AWS Simple Email Service (SES) with Laravel: A Comprehensive Guide
Favicon
The Differences In Sending Email Actions Between SES Version 1 and Version 2 APIs
Favicon
Unlocking Real-Time Insights: Harnessing Lambda and SES to Supercharge S3 Bucket Alerts
Favicon
Tracking Email Activity from AWS Simple Email Service (SES)
Favicon
Streamline Email Sending with AWS SES, Lambda, and S3 Integration
Favicon
Setup WordPress using AWS Lightsail
Favicon
Getting Started with SES: Required Permissions to Send Emails
Favicon
AWS SES - Core Concepts

Featured ones: