AMPScript: What Is AMPScript? Resources and Examples

My firm is building out dynamic emails that are driven by preference pages built in Cloud Pages using AMPScript for multiple Marketing Cloud clients, most of whom are integrated with Salesforce as their CRM. When we begin working with Marketing Cloud clients, we’re often stunned that they’re not taking advantage of this powerful customization tool to create targeted and personalized emails that are far more engaging. Simply put, you’re most likely not realizing your return on investment with Marketing Cloud if you’re not deploying solutions that utilize AMPScript.

What Is AMPScript?

AMPScript is a proprietary scripting language native to Salesforce Marketing Cloud that is used to insert personalized and dynamic content into email messages, landing pages, and SMS messages.

There’s no documentation on why it’s called AMPScript… although it may have been something like Automated Marketing Cloud Programming Script. And it’s not to be confused with Adobe’s Marketing Cloud Script, AMP.

What Are Some AMPScript Learning Resources?

Is There An AMPScript Editor?

One reason many companies avoid using AMPScript is that, while powerful, there are limited resources to edit and test AMPScript… so it can be quite frustrating. With no native editor in the platform that autocompletes and verifies your syntax for errors, Marketing Cloud users are forced to use third-party solutions or simply write and test, write and test, write and test. I’m not seeing a change to this any time soon, so we’re going to provide some resources and examples here that may be helpful.

AMPScript Examples

Here’s a simple example of a personalized message that is built with AMPScript that pulls first and last name from your Marketing Cloud subscriber attributes:

%%[

/* Set variables for personalized content */

SET @firstName = AttributeValue("FirstName")
SET @lastName = AttributeValue("LastName")

/* Insert personalized content into email message */

Hi @firstName,

Thank you for signing up for our newsletter! We hope you find the content valuable.

Sincerely,
The @lastName Family

]%%

Here’s an example where we can dynamically change the content of the message by looking up the interest attribute of a subscriber

%%[

/* Set variables for dynamic content */

SET @interest = AttributeValue("Interest")

/* Display content based on subscriber's interest */

IF @interest == "Sports" THEN
  Output(Concat("Check out our latest sports news and updates!"))
ELSEIF @interest == "Technology" THEN
  Output(Concat("Stay up-to-date on the latest tech trends and innovations with our newsletter!"))
ELSE
  Output(Concat("Discover a wide range of topics in our newsletter!"))
ENDIF

]%%

Or, if you have a comma-separated attribute of interests, you can see if your term is included in the attribute:

%%[

/* Set variables for personalized content */

SET @interestList = AttributeValue("InterestList")
SET @term = "Technology"

/* Check if term exists in interest list */

IF IndexOf(@interestList, @term) > 0 THEN
  Output(Concat("You are interested in technology!"))
ELSE
  Output(Concat("Your interests are not related to technology."))
ENDIF

]%%

You can even loop through a data extension to retrieve and display a specific number of records.

%%[

/* Declare variables for personalized content */
var @rows, @row, @rowCount, @numRowsToReturn, @lookupValue, @i

/* Set variables for personalized content */
set @lookupValue = "Shirts"
set @numRowsToReturn = 3 /* 0 means all, max 2000 */

/* Query and retrieve the rows of data as well as their order */
set @rows = LookupOrderedRows("Orders",@numRowsToReturn,"OrderDate desc, ProductName asc", "ProductType", @lookupValue)
set @rowCount = rowcount(@rows)

/* Display each of the rows */
if @rowCount > 0 then

  for @i = 1 to @rowCount do

    var @ProductName, @OrderDate
    set @row = row(@rows,@i) /* get row based on counter */
    set @ProductName = field(@row,"ProductName")
    set @OrderDate = field(@row,"OrderDate")

    ]%%

    Row %%=v(@i)=%%, %%=v(@ProductName)=%% was ordered on %%=v(@OrderDate)=%%

    %%[ 
  next @i ]%%

%%[ else ]%%

No shirt orders found

%%[ endif ]%%

And, of course, you can write HTML within the output of your AMPScript to create beautiful, dynamic HTML messages for your subscribers.

If you’re seeking assistance on development of your dynamic HTML email, dynamic SMS messages, or dynamic landing pages built on Cloud Pages, don’t hesitate to reach out to DK New Media for assistance.

Exit mobile version