AMPscript functions provide a versatile and powerful way to create highly personalised content in your Salesforce Marketing Cloud campaigns. In this blog post, we will explore a variety of AMPscript functions that can be used for data manipulation, date and time, and string operations. We will also delve into the practical use cases for these functions and demonstrate how to implement advanced personalisation techniques using AMPscript.
Data manipulation functions Understanding the role of data manipulation functions in AMPscript
Data manipulation functions in AMPscript allow you to modify, combine, or extract data from your subscriber data or other sources, enabling you to create highly personalised content.
1 - Examples of data manipulation functions:
The Concat function is used to combine two or more strings together. Here's an example of how you can use Concat to merge a subscriber's first name and last name into a single full name:
%%[ var @firstName, @lastName, @fullName set @firstName = "John" set @lastName = "Doe" set @fullName = Concat(@firstName, " ", @lastName) ]%% Hello, %%=v(@fullName)=%%!
The Replace function is used to replace a portion of a string with a new string. In this example, we replace the word "Friend" with the subscriber's first name:
%%[ var @greeting, @firstName set @greeting = "Hello, Friend!" set @firstName = "John" set @greeting = Replace(@greeting, "Friend", @firstName) ]%% %%=v(@greeting)=%%
The Substring function extracts a portion of a string, given a starting position and length. In this example, we extract the first three characters of a subscriber's last name:
%%[ var @lastName, @shortName set @lastName = "Doe" set @shortName = Substring(@lastName, 1, 3) ]%% Your short name is %%=v(@shortName)=%%.
2 - Date and time functions
Overview of date and time functions in AMPscript
Date and time functions in AMPscript allow you to work with date and time values, such as formatting, calculating differences, and displaying localised dates.
Examples of date and time functions:
The Now function returns the current date and time. In this example, we display a personalised message with the current date:
%%[ var @currentDateTime set @currentDateTime = Now() ]%% Today's date is %%=FormatDate(@currentDateTime, "MMMM d, yyyy")=%%.
The DateAdd function adds or subtracts a specified time interval from a date. In this example, we calculate a subscriber's date of birth based on their age:
%%[ var @age, @dateOfBirth set @age = 30 set @dateOfBirth = DateAdd(Now(), -1 * @age, "Y") ]%% Your date of birth is %%=FormatDate(@dateOfBirth, "MMMM d, yyyy")=%%.
The FormatDate function formats a date value using the specified format. In this example, we display a localised date for a subscriber's last purchase:
%%[ var @lastPurchaseDate set @lastPurchaseDate = "2020-04-10T12:34:56Z" ]%% Your last purchase was on %%=FormatDate(@lastPurchaseDate,"MMMM d, yyyy", "hh:mm tt", "en-US")=%%.
3 - String operations functions
Introduction to string operations functions in AMPscript
String operations functions in AMPscript help you modify and manipulate text strings, which can be useful for formatting subscriber data or creating personalised content.
Examples of string operations functions:
The Uppercase function converts a string to uppercase. In this example, we transform the subscriber's last name to uppercase:
%%[ var @lastName, @upperLastName set @lastName = "Doe" set @upperLastName = Uppercase(@lastName) ]%% Your last name in uppercase is %%=v(@upperLastName)=%%.
The Lowercase function converts a string to lowercase. In this example, we transform the subscriber's first name to lowercase:
%%[ var @firstName, @lowerFirstName set @firstName = "John" set @lowerFirstName = Lowercase(@firstName) ]%% Your first name in lowercase is %%=v(@lowerFirstName)=%%.
The ProperCase function capitalises the first letter of each word in a string. In this example, we transform a subscriber's full name into proper case:
%%[ var @fullName, @properFullName set @fullName = "john doe" set @properFullName = ProperCase(@fullName) ]%% Your full name in proper case is %%=v(@properFullName)=%%.
4 - Practical examples of personalisation with AMPscript functions
a. Personalising email subject lines using data manipulation functions:
You can use AMPscript functions to create dynamic subject lines that grab your subscribers' attention. In this example, we personalise the subject line based on the subscriber's name and a special offer:
%%[ var @firstName, @offer, @subjectLine set @firstName = "John" set @offer = "20% off" set @subjectLine = Concat(@firstName, ", don't miss out on ", @offer, "!") ]%% Subject Line: %%=v(@subjectLine)=%%
b. Displaying dynamic content based on the subscriber's last purchase date:
With AMPscript functions, you can tailor your email content to each subscriber's purchase history. In this example, we show a message based on the time elapsed since the subscriber's last purchase:
%%[ var @lastPurchaseDate, @daysSinceLastPurchase set @lastPurchaseDate = "2020-04-10T12:34:56Z" set @daysSinceLastPurchase = DateDiff(@lastPurchaseDate, Now(), "D") ]%% %%[ if @daysSinceLastPurchase < 30 then ]%% Thank you for your recent purchase! We hope you're enjoying your new item. %%[ elseif @daysSinceLastPurchase >= 30 and @daysSinceLastPurchase < 90 then ]%% It's been a while since your last purchase. Check out our latest offers! %%[ else ]%% We miss you! Here's a special offer just for you: Save 25% on your next purchase. %%[ endif ]%%
c. Generating a unique promo code for each subscriber using string operations functions:
To create a sense of exclusivity and encourage subscribers to take action, you can generate a unique promo code for each subscriber using AMPscript functions:
%%[ var @firstName, @lastName, @promoCode set @firstName = "John" set @lastName = "Doe" set @promoCode = Concat(Substring(@firstName, 1, 1), Substring(@lastName, 1, 1), FormatDate(Now(), "MMdd")) ]%% Your unique promo code is %%=v(@promoCode)=%%.
Tips for implementing advanced personalisation techniques with AMPscript functions
a. Leveraging nested functions for complex personalisation scenarios:
AMPscript allows you to nest functions within other functions, enabling you to create more intricate personalisation scenarios. In this example, we generate a personalised message based on the time of day:
%%[ var @currentTime, @timeOfDay, @message set @currentTime = Now() set @timeOfDay = DatePart(@currentTime, "h") if @timeOfDay >= 6 and @timeOfDay < 12 then set @message = "Good morning" elseif @timeOfDay >= 12 and @timeOfDay < 18 then set @message = "Good afternoon" else set @message = "Good evening" endif ]%% %%=v(@message)=%%, %%=v(ProperCase("john doe"))=%%!
b. Utilising conditional statements and loops to create targeted messaging:
By combining AMPscript functions with conditional statements and loops, you can create highly targeted and dynamic content. In this example, we display a list of recommended products based on a subscriber's previous purchases:
%%[ var @products, @productCount set @products = BuildRowsetFromString("Product A,Product B,Product C", ",") set @productCount = RowCount(@products)]%% Here are some recommended products for you: <ul> %%[ for @i = 1 to @productCount do ]%% %%[ set @product = Field(Row(@products, @i), 1) ]%% <li>%%=v(@product)=%%</li> %%[ next @i ]%% </ul>
c. Combining multiple AMPscript functions for powerful personalisation effects:
Using multiple AMPscript functions together allows you to create even more personalised content. In this example, we generate a custom expiration date for a subscriber's promo code:
%%[ var @promoCode, @expirationDate set @promoCode = "ABCD1234" set @expirationDate = DateAdd(Now(), 7, "D")]%% Your promo code (%%=v(@promoCode)=%%) expires on %%=FormatDate(@expirationDate, "MMMM d, yyyy")=%%. Don't miss out!
By exploring the capabilities of AMPscript functions and their practical applications, you can unlock new opportunities for personalisation in your email marketing campaigns. This deep dive into data manipulation, date and time, and string operations functions provides valuable insights into how you can create more engaging, relevant content for your audience. As you continue to experiment with AMPscript functions and implement advanced personalisation techniques, you'll be well-equipped to elevate your email marketing efforts and achieve outstanding results.
If you need any assistance or support with your AMPscript implementation, don't hesitate to contact us today. Our team of experts is ready to help you harness the full potential of Salesforce Marketing Cloud and AMPscript to achieve your marketing goals.