Navigate back to the homepage

CloudPage Deep Linking

Jason Hanshaw
January 1st, 2020 · 3 min read

I know what you’re thinking: SFMC already has deep linking configurations built-in, so why do I need this? Well, maybe you weren’t thinking that, but it’s a great question!

While using features, such as iOS Universal Links, might be sufficient for most users, there are use-cases in which the desired functionality requires more flexibility. Let’s say we want custom routing based on device type or a universal link for both Android and iOS devices. How can we determine the device type and direct users where we want them to go? In order to implement that functionality, we’ll need to turn to user agents.

User Agent

A user agent is a string that your internet browser sends to the web server it’s communicating with, describing your operating system, browser, rendering engine, and other important details. This information is passed to web servers via HTTP headers and can be used to dynamically render content based on the type of device or OS that the request is being made from. To get a better idea of what this looks like, let’s take a look at the following example:

Mozilla/5.0 (Windows NT 10.0; Win 64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.88 Safari/537.36

Browser: Chrome (version 79; patch 3945)
Device: PC (Windows 10 64-bit)
Rendering Engine: AppleWebKit

By parsing the above user agent string, we were able to extract the browser, device type and rendering engine of the request. For more information on user agents, or to see more examples, please refer to the [MDN document]( agent) on this subject.

How Can We Use This Information?

So, now that we have found a way to get a user’s browser and device type, we can start to implement our solution. First, we’re going to need a script for retrieving, and parsing, the user agent in order to determine where the request is coming from.

Creating the Device Detection Script

First, let’s create a Javascript Code Resource CloudPage in order to house our user agent detection script. Once that’s set up, we’ll drop in the following script and save the page.

1DeviceDetection = function(ua) {
3 this.checks;
4 this.construct = function(ua) {
5 if (typeof ua == 'undefined') {
6 var ua = navigator.userAgent;
7 }
8 = ua;
9 // parse data
10 this.checks = {
11 iphone: Boolean(ua.match(/iPhone/)),
12 ipod: Boolean(ua.match(/iPod/)),
13 ipad: Boolean(ua.match(/iPad/)),
14 blackberry: Boolean(ua.match(/BlackBerry/)),
15 playbook: Boolean(ua.match(/PlayBook/)),
16 android: Boolean(ua.match(/Android/)),
17 macOS: Boolean(ua.match(/Mac OS X/)),
18 win: Boolean(ua.match(/Windows/)),
19 mac: Boolean(ua.match(/Macintosh/)),
20 wphone: Boolean(ua.match(/(Windows Phone OS|Windows CE|Windows Mobile)/)),
21 mobile: Boolean(ua.match(/Mobile/)),
22 androidTablet: Boolean(ua.match(/(GT-P1000|SGH-T849|SHW-M180S)/)),
23 tabletPc: Boolean(ua.match(/Tablet PC/)),
24 palmDevice: Boolean(ua.match(/(PalmOS|PalmSource| Pre\/)/)),
25 kindle: Boolean(ua.match(/(Kindle)/)),
26 otherMobileHints: Boolean(ua.match(/(Opera Mini|IEMobile|
27 SonyEricsson|smartphone)/)),
28 };
29 }
30 this.isTouchDevice = function() {
31 return this.checks.iphone || this.checks.ipod || this.checks.ipad;
32 }
34 this.isApple = function() {
35 return this.checks.iphone || this.checks.ipod || this.checks.ipad
36 || this.checks.macOS || this.checks.mac;
37 }
39 this.isBlackberry = function() {
40 return this.checks.blackberry;
41 }
43 this.isAndroid = function() {
44 return;
45 }
47 this.isTablet = function() {
48 return this.checks.ipad || this.checks.tabletPc || this.checks.playbook
49 || this.checks.androidTablet ||;
50 }
51 this.isDesktop = function() {
52 return !this.isTouchDevice() && !this.isSmartPhone() && !this.isTablet()
53 }
54 this.isSmartPhone = function() {
55 return ( || this.checks.blackberry || this.checks.palmDevice
56 || this.checks.otherMobileHints) && !this.isTablet() && !this.checks.ipod;
57 }
59 this.construct(ua);

In the above script we are doing a few different things. First, we’re grabbing the user agent from the HTTP header and assigning it to a variable using var ua = navigator.userAgent;. Then, we use regular expressions to check each device type to determine if the user agent string contains matches that identify it as that particular type. Once we have ran this check, we’ll use some simple functions to categorize our device type into a more general classification that we can use to direct users.

Now that we’ve got a script to retrieve the user agent and to categorize the device, we’ll need to create another CloudPage that will actually redirect the user to the correct location.

Creating Redirection CloudPage

For this step, we’ll set up a standard CloudPage and then drop in this markup that will allow us to run the script from the previous step and then direct the user to the right location based on its results.

2 <body>
3 <!--[if IE]>
4 <script type="text/javascript">
5 window.location = '';
6 </script>
7 [endif]-->
8 <script src=""></script>
9 <script src="deviceDetection.js"></script>
10 <script type="text/javascript">
11 $(function() {
12 var detection = new DeviceDetection(navigator.userAgent);
13 if (detection.isApple() && !detection.isDesktop()) {
14 document.location = 'sampleapp://mobile/?fid=CI&confirmationno=';
15 setTimeout(function() {
16 window.location = '';
17 }, 500);
18 }
19 if (detection.isAndroid()) {
20 document.location = 'sampleapp://mobile/?fid=CI&confirmationno=%';
21 setTimeout(function() {
22 window.location = '';
23 }, 800);
24 }
25 if (detection.isDesktop()) {
26 window.location = '';
27 }
28 });
29 </script>
30 </body>

There are a few pieces to unpack in this markup, so let’s break them down into separate snippets and analyze each one:

1<!--[if IE]>
2 <script type="text/javascript">
3 window.location = '';
4 </script>

Here we’re going to isolate IE users due to browser incompatibility with our approach. These users will automatically be redirected to our app landing page.

1var detection = new DeviceDetection(navigator.userAgent);
2if (detection.isApple() && !detection.isDesktop()) {
3 document.location = 'sampleapp://mobile/?fid=CI&confirmationno=';
4 setTimeout(function() {
5 window.location = '';
6 }, 800);

In this portion, we’re going to initialize our user agent string detection script and determine if the device type is an apple device but not a desktop computer. We’ll also use the setTimeout() function in order to determine whether the user has our application installed on their phone currently. If the redirection to the mobile app deep link is not successful after 800 milliseconds, then we’ll revert to the iTunes store for our application. This ensures that we provide a suitable fallback for users who don’t already have our application or if there is an issue connecting to the app.

1if (detection.isAndroid()) {
2 document.location = 'sampleapp://mobile/?fid=CI&confirmationno=%';
3 setTimeout(function() {
4 window.location = '';
5 }, 800);

This snippet is similar to the previous one in functionality except that we are now checking whether the request is coming from an Android device rather than an apple one. Note that, because Apple operates both desktop and mobile devices while Android is mobile-only, the isDesktop() check is not necessary for this type.

1if (detection.isDesktop()) {
2 window.location = '';

Finally, we’re checking to see if the user device type is a desktop computer. If so, we’ll redirect them to our application landing page so that they can get further information about our sample app.

Now that we have our detection script and redirect CloudPage in place, it’s time to add this functionality to our email send! To do this, all we need to do is identify the button or link that you would like to deep link and substitute your existing URL for the following (assuming the CloudPage ID is 123 in this example):



That’s it! With this solution, we are now able to dynamically deep link our user’s to the appropriate version of our application while having a solid fallback in place to handle any contingencies. In addition to this blog post, you can find the code for this example in this github repository.

More articles from InvokeCreate

Visualize Data Extension Rows With Chart.js

Display Aggregated Data Extension Data Using CloudPages With Chart.js

June 11th, 2020 · 4 min read

Create A Searchable Content Directory With CloudPages

Use SSJS, VueJS and Code Resources in order to create a searchable index of HTML Content Blocks.

May 10th, 2020 · 3 min read
© 2022 InvokeCreate
Link to $ to $ to $