A case for the MicrositeURL() function

Do you know about the MicrositeURL() AMPScript function? It’s relatively new. It sure is handy if you’re building custom preference centers in Enterprise 2.0 business units.

The idea is that you can encrypt custom parameters in your email links to Microsite pages and then retrieve the unencrypted values with an AMPScript function. Once you have the business unit MID in the microsite page, you can use it to ensure your API calls are for the correct business unit.

In your email you would have this code:

In the case where the the subscriberMID is not set, default to the parent business unit’s MID, which is accessible from the memberid personalization string.

One gotcha: If you build your custom preference centers with Server-Side Javascript, there’s no SSJS function to retrieve the additional MicrositeURL parameters — you to use AMPScript. Your code may end up looking something like this:

For further reading:
MicrositeURL()
RequestParameter()
Personalization Strings
Server-Side JavaScript

Regular Expressions in AMPScript

Did you know you can use regular expressions in AMPScript?

You can.

Here are some RegExMatch() snippets that I’ve used:

Variables

@s = input string
@p = pattern
@m = match
@o = output

Sample Code

Output

Strip leading zeroes from a string

input: 0000012345
pattern: “^0*(\d+)$”
output: 12345

Check for all digits, no match

input: 12345x
pattern: “^\d*$”
output:

Check for all digits, match

input: 12345
pattern: “^\d*$”
output: 12345

Replace parenthetical text with space

input: whee (whatever it is) whoop
match: (whatever it is)
pattern: “\s(.+)\s”
output: whee whoop

For further reading

RegExMatch() function
Concat() function
OutputLine() function
RegEx pattern reference
RegEx options reference

Flexible Content Areas with XML Interfaces

Here is a good way to display a grid of external content in an email without repeating a significant amount of AMPScript. It utilizes an XML string and uses XPATH to retrieve content using the BuildRowSetFromXML() function.

Slide deck from my Salesforce Marketing Cloud/ExactTarget Connections Conference 2014 presentation

Helpful Links/Resources

Double Wide v2 – Responsive Email Pattern
XPATH reference
All AMPScript Functions
LookupOrderedRows() Function
BuildRowsetFromXML() Function
TreatAsContent() Function
ContentAreaByName() Function

Product Data Extension

SKUProductImageURLAvgCustomerRatingApprovedForEmail
1234http://placehold.it/150x150&text=12344true
2468http://placehold.it/150x150&text=24683true
1357http://placehold.it/150x150&text=13575true

Email Init Section

Email Body

Content Area Init Section

Content Area Body

Questions? Comments? Feel free to leave a comment below.

Adding Rows to Data Extensions

Data Extensions in the Salesforce Marketing Cloud (ExactTarget) platform are simply database tables in which you can store data you need for your campaigns.

You can add rows to data extensions using:

With the API, you have a few options — SOAP being the most complex, AMPScript the simplest:

You can use AMPScript in emails, landing pages or SMS keyword configurations. I’ll use AMPScript in this example.

Here’s how you add or update a single row in a data extension named My_Test_DataExtension using the UpsertDE() function:

Here’s a breakdown of the UpsertDE() arguments in this example:

  1. The name of the data extension to receive the data
  2. Number of columns you’re using to check to see if there’s a matching row already in the data extension. (This means there will be only 1 pair of name and value arguments following this parameter.  You could specify more.)
  3. The lookup column
  4. The lookup value
  5. Column 1 to update
  6. Column 1 update value
  7. Column 2 to update
  8. Column 2 update value

For further reading:
UpdateDE()
AMPScript Functions
UpsertData() (for SMS keywords)
UpdateData() (for SMS keywords)



Coupon Claiming in AMPScript

One cool thing you can do in the Salesforce Marketing Cloud is offer your subscribers a unique coupon code as part of a promotional email campaign. Here’s how to do it.

To start, create a “CouponCodes”Data Extension with the following columns:
coupon-claimrow2

Next, generate your own list of unique coupon codes in Excel. Include a “CouponCode” column header. Save the worksheet as CSV or Unicode (tab-delimited).
coupon-claimrow1

Then, import your coupons into the data extension with the adhoc import button:
coupon-claimrow3

Finally, add this AMPScript to your email. This code will try to find a coupon row for the specified email address in the CouponCodes data extension. If it finds one, it’ll return the coupon code. If not, it will “claim” an unclaimed code and associate it with the specified email address. The bit about the message context — that will prevent you from claiming coupon rows when you’re doing a send preview when building the email.

Keep these things in mind:

  • Make sure you upload enough coupons for your target audience so you don’t run out of unclaimed coupons. For recurring coupon campaigns, you should build a system to monitoring unused coupon codes.
  • Make sure the target of the coupon code (e-commerce site, POS system, etc) is using the same set of coupon codes as your campaign.
  • You can also do this in an SMS campaign.
  • The data extension name in the claimrow() function cannot be a variable
  • Any data extension referenced in a claimrow() function must exist

For further reading:

Data Extensions
Data Extension AMPScript Functions
MobileConnect Coupon Scenario
Live Offers

Update 5/28/2013: added additional gotchas about claimrow() function
Update 2/9/2015: added empty() check after claimrow() function
Update 3/9/2016: move code snippet to gist, added variable declaration for emailaddr