How to Use Marketing Cloud to Build Your Own Page Redirects for Salesforce Survey Links

Salesforce Architects
Salesforce Architects
5 min readOct 15, 2020

--

Salesforce survey invitation URLs are typically hundreds of characters long, which can create issues for anyone attempting to send them to mobile devices via SMS messages:

  • SMS messages that are more than 160 characters long get split into multiple messages, which increases costs since messaging charges are based on volume sent.
  • URLs that approach or exceed SMS limits won’t have room for any additional message text.
  • Recipients who aren’t using smartphones are unable to directly open survey links that span multiple messages.

Third-party URL shorteners may solve these issues on a smaller scale, but they become costly as message volumes increase. This issue used to be fairly rare, but it’s becoming increasingly common. Universities, for example, are using tools like Work.com to administer daily wellness check surveys to tens of thousands of students as part of their campus reopening protocols. Large corporations with tens of thousands of employees are facing similar issues. In these scenarios, having to pay for multiple SMS messages to every recipient every time a survey is sent out (which is usually daily) will cause messaging costs to skyrocket. And the fees required to have a third-party system shorten tens of thousands of URLs each day aren’t much better.

If you find yourself in a scenario like this, you can use the approach described in this post to shorten the survey invitation URLs (or any other URLs) yourself without having to rely on a third party.

To start the process, you’ll need to know the URL itself along with a unique ID that can be used to represent it. For a survey invitation URL, you can use the Survey Invitation record’s 18-character Salesforce ID (from the SurveyId field), which can be sent to Marketing Cloud through Marketing Cloud Connect.

Within Marketing Cloud, you can build a data extension to store both of these values prior to initiating your send and create a CloudPage to handle the redirect code (more on that below). When you compose your SMS message, instead of including the full survey invitation URL, include the URL for your CloudPage with the survey invitation ID as a parameter.

When a recipient opens the SMS message and taps the link, their browser opens the CloudPage, which will use the survey invitation ID to retrieve the full URL from the data extension via AMPscript and then use JavaScript to execute a client-side redirect. The entire process typically takes less than a second.

A flow diagram of the navigation flow from clicking the link to the destination survey page

You’ll need to create the “URLShortener” data extension prior to creating your redirect CloudPage. This is a fairly simple data extension with just two columns:

  1. URLID — text(18) and primary key
  2. URL — text(4000).

You can find an example that shows the AMPscript and HTML for handling the redirect in this GitHub Gist. Let’s take a closer look at how this code works. Remember, though, to set up your data extension before deploying the HTML to a CloudPage.

Lines 4–12: Variable initialization

<!-- %%[
Set @SurveyURL = 'https://www.salesforce.org'
Set @SurveyID = RequestParameter("s")
If length(@SurveyID) > 0 Then
Set @SurveyRows = LookupOrderedRows("URLShortener",1,"URL ASC","URLID",@SurveyId)
Set @Row = Row(@SurveyRows,1)
Set @SurveyURL = Field(@Row,"URL")
ENDIF
]%% -->

This section of AMPscript processes the shortened URL and sets the variable @SurveyID with the value passed through the s parameter. It’s a good practice to set a default value for @SurveyURL; this will help handle a scenario where the page is loaded without the proper attributes by taking the user to a default website.

Next, if a SurveyID is found in the shortened URL the AMPscript will look it up in the “URLShortener” data extension using the @SurveyID variable. A best practice here is to use the LookupOrderedRows() AMPscript method because of the way it handles multiple values. However, the lookup() method should also work if the @SurveyID is set as a primary key on the data extension.

Finally the @SurveyURL variable is set after the value from the URLShortener table is retrieved.

Lines 14 and 15: Meta tags

<meta property="og:title" content="Redirect Demo" />
<meta property="og:image" content="https://image.s4.sfmc-content.com/lib/fe3611727664047f711174/m/1/64b290b1-e371-4a22-86c7-aed962e5e80e.png" />

These meta tags are important for text messaging. If a user has the SMS sending number saved as a contact in their phone, then its associated image and title will display on most smartphones. Important: If you use the sample code, make sure to replace these tags with your own content when setting up this page.

Lines 16–32: CSS

Your CloudPage will only be displayed for a fraction of a second before the redirect kicks in so most users will never see it, but adding some formatting here will make it look professional in case there’s ever a network issue or other type of delay that causes them to see it for longer than anticipated.

Lines 34–41: JavaScript redirect function

%%[IF length(@SurveyID) > 0 THEN]%%
<script>
window.onload = function() {
myFunction()};
function myFunction() {
location.replace("%%=v(@SurveyURL)=%%")
}
</script>

When the window loads, this function replaces the current location or URL with the contents of the @SurveyURL variable. It’s crucial for this not to happen until after the AMPscript runs. AMPscript executes on the server side, and if the URL hasn’t already been retrieved, the JavaScript will have no way to retrieve it on the client side. To provide some additional assurance, the JavaScript in this sample code is surrounded by an AMPscript condition that prevents it from executing if the SurveyID does not exist.

Lines 43–73: Error handling

The remaining HTML is used to build a page that will display if the JavaScript fails to execute. The @SurveyURL is set as a tag to the image in line 53 and as a call-to-action link in line 65. Line 54 contains an img tag for branding, along with h1 and h3 tags to tell the recipient what to do if they get stuck on this page. Again, if you use the sample code make sure to update the text here with your own instructions.

Next steps

Now that you know how to build your own page redirects with CloudPages, AMPscript, and JavaScript you may want to take some time to explore AMPscript and the programmatic languages for Marketing Cloud further.

About the Author

Author Josh Zimmerman

Josh Zimmerman is a Marketing Cloud Customer Success Architect at Salesforce.org. He helps nonprofit and higher education organizations harness the power of Marketing Cloud to communicate effectively with their constituents. Connect with Josh on LinkedIn.

--

--

Salesforce Architects
Salesforce Architects

We exist to empower, inspire and connect the best folks around: Salesforce Architects.