Advanced Guide to Link Shortening and Link Tracking with Twilio Messaging

July 13, 2023
Written by
Twilion
Reviewed by
Twilion

Advanced Guide to Link Shortening and Link Tracking with Twilio Messaging

If you're facing issues with long URLs in SMS messages, or you want to use your company domain for better branding and click tracking, Twilio's Link Shortening feature can help.

This functionality enables you to shorten long links with your company-branded domain programmatically without any extra API calls. You can also set up your webhook to receive click events when recipients of the messages click on the shortened link.

This tutorial guides you through setting up link shortening and click-tracking using Twilio and DNS providers, let’s get started!

Overview

This how-to guide in the Twilio Docs provides instructions for setting up Link Shortening and Click Tracking using Twilio. However, it does not include samples for domain verification and SSL setup. To complement that guide, I have added samples that cover registering and verifying domains, adding DNS records, generating TLS certificates, and configuring click tracking. The document also includes instructions for testing the shortened links using Twilio's built-in test UI or through the API.

Prerequisites

Setup step-by-step

Step 1: Setting up an Organization

Link Shortening requires you to set up an Organization in the Twilio Admin Center. This can be accessed and set up via the console. More information regarding Organizations can be found here.

Step 2: Register domains in the Twilio console and DNS console (Alibaba Cloud)

The DNS provider used in this case is Alibabacloud.com, but other providers should have similar settings for editing DNS entries.

I'll use the domain midshipman.xyz as an example, and I'll use the sub-domain, twilio.midshipman.xyz, for this guide. Where you see me use either entry, please substitute your domain or subdomain in the step.

To proceed, follow the initial steps from the link provided here. After setup, you should see twilio.midshipman.xyz in your Twilio Console, and the _twilio.twilio record in your DNS console, similar to what you can see shown in the screenshots below. It usually takes only a few minutes for the domain to be verified.

Verify domains inside the Twilio Console

 

Verify domains with your DNS provider

 

Step 3: Adding DNS record

To add twilio.midshipman.xyz as a sub-domain, create a CNAME record in DNS and point it to lsct.ashburn.us1.twilio.com. After adding the record, your DNS settings should resemble the screenshot below:

add DNS record

 

Step 4: Generating a TLS Certificate

To create a free TLS certificate for twilio.midshipman.xyz, begin by registering an account on sslforfree.com/ (or your choice of SSL provider). The process should be straightforward.

After registering, verify the domain for the certificate in DNS, similar to as shown in the screenshot below.

Verify the certificate for the domain in your DNS console

 

 

 

Now go back to sslforfree.com console to download the certificate.

Download a TLS certificate from SSL For Free

 

Step 5: Uploading TLS Certificate

The private.key from step 4 is in PKCS #1 format if you used SSL for Free. If that's the case for your provider, convert it to PKCS #8 using the command below, then upload the resulting file to Twilio in your Console.


openssl pkcs8 -in private.key -out private_pkcs8.pem -topk8 -nocrypt

Step 6: Domain configurations with click tracking

https://beeceptor.com/ is a useful tool for setting up webhooks and inspecting HTTP traffic. The callback URL for click tracking in this demo is linktracker.free.beeceptor.com – it will receive the link clicked events for my demo. You can see the finished sample settings in the screenshot below:

Click tracking settings for testing in Beepceptor

You can use the built-in test GUI in the Twilio console to send an SMS with the link as shown below:

Send a test SMS from the Twilio Console

If you want to use the API, don’t forget to set the parameter ShortenUrls=true. Check this link for more details.

Receive the SMS with the shortened link on the phone:

 

Receive a short link in the SMS

 

If you click the link, the original URL will be opened in the browser and you can see the click event on beeceptor.com as shown below:

Original link opened in the browser

 

event tracking

 

Conclusion

Setting up Link Shortening with Twilio can be a great way to improve branding and track clicks on your links. By following the steps outlined in this guide, you can set up Link Shortening using your own company branded domain in a straightforward way. Don't forget to test the shortened links using Twilio's built-in test UI or through the API to ensure everything is working as intended.

Hao Wang is a principal solution engineer at Twilio to help customers make the best use of Twilio products. He can either be reached at hwang0 [at] twilio.com or LinkedIn.