Personalization strings using AmpScript
AMPscript is a powerful scripting language in Salesforce Marketing Cloud (SFMC) that allows for dynamic content personalization in emails, SMS, and landing pages. It can be used to insert personalized data, apply conditional logic, and interact with Data Extensions.
- Basic Personalization Using AMPscript
AMPscript can be used to personalize email subject lines, greetings, or body content.
Example: Personalizing the First Name
%%= v(@FirstName) =%%
This pulls the FirstName value from a Data Extension or List Attribute.
Example: Using a Default Value (Fallback)
%%[
SET @FirstName = AttributeValue(“FirstName”)
]%%
Hello %%= v(@FirstName) =%%!
- If FirstName exists, it displays the subscriber’s name.
- If FirstName is missing, it can be set to a default value (e.g., “Customer”).
- Conditional Personalization (IF-ELSE Statements)
AMPscript allows conditional statements to show different content based on subscriber attributes.
Example: Display Different Greetings Based on Gender
%%[
IF Gender == “Male” THEN
SET @greeting = “Mr.”
ELSEIF Gender == “Female” THEN
SET @greeting = “Ms.”
ELSE
SET @greeting = “Dear Customer”
ENDIF
]%%
Hello %%= v(@greeting) =%% %%= v(@LastName) =%%,
- If Gender = Male, the email says “Hello Mr. LastName”.
- If Gender = Female, it says “Hello Ms. LastName”.
- If gender is missing, it defaults to “Hello Dear Customer”.
- Using Data Extensions for Personalization
You can retrieve data from a Data Extension (DE) using Lookup() or LookupRows().
Example: Fetching a Subscriber’s Loyalty Points
%%[
SET @points = Lookup(“LoyaltyProgramDE”, “Points”, “Email”, _subscriberKey)
]%%
Your current loyalty points: **%%= v(@points) =%%**
- This retrieves Points from the LoyaltyProgramDE where the email matches _subscriberKey.
- Creating Personalized URLs
AMPscript can dynamically generate customized URLs with subscriber data.
Example: Personalized Discount Link
%%[
SET @discountCode = “WELCOME2024”
SET @customerID = _subscriberKey
]%%
Click here to redeem your discount:
[Claim Offer](https://example.com/redeem?code=%%=v(@discountCode)=%%&id=%%=v(@customerID)=%%)
- This generates a unique URL with a discount code and the customer’s ID.
- Dynamic Email Subject Lines
AMPscript can customize subject lines for better engagement.
Example: Subject Line Personalization
%%[
SET @subject = “Exclusive Offer Just for You, ” + FirstName
]%%
In the subject line field, use:
%%= v(@subject) =%%
- If FirstName = John, the subject becomes “Exclusive Offer Just for You, John”.
- Using AMPscript in Emails
AMPscript can be used in two ways:
- Inline AMPscript (For Simple Personalization)
Hello %%= v(FirstName) =%%, welcome to our loyalty program!
- Block AMPscript (For Complex Logic)
%%[
SET @tier = Lookup(“LoyaltyDE”, “Tier”, “Email”, _subscriberKey)
IF @tier == “Gold” THEN
SET @offer = “20% discount”
ELSE
SET @offer = “10% discount”
ENDIF
]%%
Hello %%= v(FirstName) =%%, you have a %%= v(@offer) =%% on your next purchase!
- Gold members get 20% off, while others get 10% off.
Best Practices for AMPscript Personalization
- Use Fallback Values to prevent blank outputs.
- Optimize Queries when using Lookup() or LookupRows().
- Test AMPscript Emails using Preview & Test in Email Studio.
- Combine with Dynamic Content for advanced customization.