Introduction
AMPscript is Marketing Cloud’s proprietary scripting language that helps marketers enhance how they communicate with customers. It can be embedded within HTML emails, text emails, landing pages, SMS messages, and push notifications from MobilePush. You can use AMPscript to retrieve data, such as customer or production information, from data extensions to use in the body of email messages and landing pages to give recipients a personalized experience.
This blog post will explain the basics of AMPscript, variables, and data extensions, and show you how to create a personalized email message by leveraging the power of AMPscript and data extensions. At the end, you’ll see a fully rendered personalized email message. Below is an example of what AMPscript looks like in Email Studio’s Content Builder.
What are data extensions?
A data extension is a table within the Marketing Cloud database that contains your data. You can use data extensions with Marketing Cloud Email Studio to store data that you want to leverage in your email messages. With data extensions, you can:
- Import information from other systems into Email Studio
- Send a message to subscribers in a data extension
- Exclude subscribers in a data extension from a message send
- Include customer-specific information from a data extension in a message
- Collect the subscriber information of people who trigger a triggered send
- Create dynamic content using data stored in a data extension in an email content area
Below is a sample Hiking Club Membership data extension that I created for this post. It contains the subscriber information of the current members in our hiking club.
What are variables?
If you’re familiar with other programming languages, then you may already know what a variable is. For those that aren’t, a variable in AMPscript is a storage location for the information that you want to reference later within a script statement or function.
Variables are declared in your code by using the keyword Var followed by one or more comma-delimited variable name(s). Once declared, variables in AMPscript are assigned using the keyword Set followed by the variable name, equal sign, and value. A variable’s value can be a constant or an attribute. Variable names must begin with @ and include at least one other letter, number, or underscore.
Using a code snippet content block
To begin building our personalized email, let’s add a code snippet content block to our email message. A code snippet content block allows us to write our AMPscript without it getting wrapped in any extra HTML. HTML content blocks can be used to store your AMPscript, but they can also add extra HTML wrappers to your code that you don’t want in your email message. Using a code snippet content block avoids that possible issue from happening and preserves your code.
Inside the code snippet block, let’s create the container for our AMPScript by creating opening and closing brackets: %%[ the code goes here ]%%. Next, we’ll declare our variables using the Var keyword and separating each variable with a comma. We’ll use the Set command to give the variables the value of the information taken from the columns in our example data extension.
The function v
We’ll explore functions more in a later blog post, but for now, let’s just go over the function v. This function takes a variable as an input, then outputs the value of the variable. In our example, we’ll use this function to take the values that we set in our AMPscript and display them in our email body. In order to output the values of the variables that we set in our code snippet block, we’ll utilize a text content block.
AMPscript (Advanced Multi-Processing Script)
Overview: AMPscript is a scripting language designed specifically for Salesforce Marketing Cloud. It is primarily used to create personalized and dynamic content within emails and landing pages.
Key Features:
- Ease of Use: AMPscript is straightforward and easy to learn, especially for marketers who are not developers. Its syntax is simple and tailored for tasks commonly required in email marketing, such as personalization and dynamic content generation.
- Performance: AMPscript is executed server-side at the time of email send, which means it is highly optimized for speed and performance in rendering dynamic content.
- Built-In Functions: It offers a wide range of built-in functions for personalization, data retrieval from data extensions, conditional logic, date manipulation, and more. Examples include Lookup(), RowCount(), and FormatDate().
- Use Cases: Ideal for simple personalization tasks, such as inserting subscriber-specific data, dynamic text, or conditional content blocks based on profile attributes or behavioral data.
When to Use AMPscript:
- When you need to quickly implement personalization in emails or landing pages.
- For tasks involving straightforward data retrieval and insertion from data extensions.
- When you need to use functions that are specifically built for marketing tasks.
SSJS (Server-Side JavaScript)
Overview: SSJS is JavaScript-based and provides more flexibility and power than AMPscript. It is used for more advanced server-side scripting tasks within Salesforce Marketing Cloud.
Key Features:
- Flexibility and Power: SSJS offers more programming capabilities and flexibility, including the ability to write complex scripts, loop structures, and handle error checking and data manipulation.
- JavaScript Knowledge: As it is based on JavaScript, it leverages standard JavaScript syntax and methods, making it familiar to developers who already have JavaScript experience.
- Advanced Integrations: SSJS can interact with various Marketing Cloud APIs, such as the SOAP and REST APIs, to perform complex operations, like retrieving data from external sources or handling transactional processes.
- Extended Capabilities: Allows for advanced data processing, such as reading/writing JSON objects, making HTTP requests, and performing external API calls directly from within the script.
- Use Cases: Suitable for tasks that require complex logic, data processing, external integrations, or operations that are not covered by AMPscript.
When to Use SSJS:
- For more complex scripts involving advanced logic, loops, or error handling.
- When integrating with external systems or using Marketing Cloud APIs.
- When you need to manipulate data in ways that are beyond the capabilities of AMPscript.
- For use cases that require client-side scripting capabilities, as SSJS is also compatible with the client-side JavaScript engine in CloudPages.
Differences AMPscript & SSJS
AMPscript:
- AMPScript is executed by SFMC at the end of the email sent. Used for personalization, landing pages, etc
- This has a fixed set of pre-built functions to be used.
- It simply and efficiently handles inline personalization or simple IF ELSE statements.
- Execution of email using AMPscript is fast.
- Personalization String.
- The FOR loops that traverse through Data Extension
- With the help of AMPscript, we can track/ measure the impression.
SSJS:
- As said, this language is similar to JavaScript so the web developers are quite familiar.
- Web developers easily adopt this language, or I can say this becomes easier to learn because of JavaScript.
- We can use try-catch blocks
- With the help of SSJS – arrays can be used.
- EVAL/Math function can be used with the help of SSJS
- With the help of SSJS – JSON requests can be taken care of.
- In Cloud Page, the core libraries can be used.
- SSJS makes the execution of email slow.
Use Cases Or Scenarios
Below are some use cases or scenarios where we identified the preferred programmatic language along with reasons as compared to other languages:
- AMPscript should be preferred for simple inline personalization, which includes content like greetings, names, emails, etc., and simple IF ELSE loops
- If the web developers are new to both programmatic languages, then AMPscript has a shorter learning curve compared to SSJS.
- If the web developers have experience with development, then SSJS will be easier to learn and implement.
- SSJS is preferred for Landing pages and applications when there are no functions to support in AMPscript
- In general, SSJS makes the execution of emails slow and hence shouldn’t be preferred over AMPscript until there are some specific benefits.
- SSJS should be preferred over AMPscript because of its JASON handling capabilities.
In case you wish to read and try it for yourself, please check out the Trailhead module for AMPscript and SSJS. For in-depth details, for both the programmatic languages, visit the link here.
Summary
- AMPscript is ideal for straightforward, efficient personalization tasks and is easy for marketers to use without deep technical knowledge.
- SSJS is better suited for complex operations, advanced integrations, and developers familiar with JavaScript.
Contact Us
We would love to hear from you Please feel free to send us a message via the form