Practice Using AMPscript
Learning Objectives
After completing this unit, you’ll be able to:
- Write basic AMPscript code.
- Test your knowledge of AMPscript.
Practice AMPScript Using the Trailhead Simulator
Now that you understand why you would use AMPscript, along with some of the key functions, we want to give you some practice using AMPscript.
Before you get started, familiarize yourself with the Trailhead Simulator for Marketing Cloud Engagement. You use the simulator instead of a Trailhead Playground to complete the steps in this project.
The Trailhead Simulator is different from a Trailhead Playground. The simulator doesn’t store your progress or any data you enter. If you close your browser, you start from the beginning of the simulation again. You can always use the navigation controls at the bottom of the simulator to get back to where you left off.
Navigation Controls |
Description |
---|---|
(1) Left Arrow |
Go backward in the simulator. |
(2) Right Arrow |
Go forward in the simulator. |
(3) Scrub Bar |
View your progress, plus you can use the progress arrow to quickly move to a different location within the simulator. |
(4) Close Button |
Exit the simulator. Remember that if you close the simulator, you start from the beginning the next time you launch it. |
AMPscript Exercises
Now that you are familiar with the simulator, let’s get started! There are three exercises for you to complete using the simulator.
Exercise 1:
Most marketers know of at least one field in their data that is never entered correctly or is in the incorrect format. Let’s use AMPscript to fix inconsistencies or minor errors in your data.
- Launch the Trailhead Simulator.
- Click Exercise 1: Use AMPscript to Fix Capitalization, and then click Begin.
- From the Marketing Cloud Engagement Apps bar, click Content Builder and select Content Builder.
- Under Local Folders, click Simulator.
- Click the email named Welcome to Cloud Kicks.
- Click Edit and select Edit Content.
- Click the Preview and Test tab.
- Click , expand the Data Extensions folder, and then expand the Simulator folder.
- Select the data extension, Sample_AMPscript.
- Click the line entry for MemberID 345678 (Tyler Oliver), and then click Select. Review the recipient information and notice:
- The last name renders with no capitalization.
- Next, click the Content tab.
- Hover over the Code Snippet section of the email, and then click the grey Code Snippet (Click to edit) area.
- Locate
%%=v(@lname)=%%
and replace with%%=ProperCase(@lname)=%%
and then Enter. - Click Done Editing.
- Return to Preview and Test and view the updated last name.
Voila! The customer's last name is now in the correct case.
Exercise 2:
Now for another handy exercise. It’s February and you realize that your copyright year hasn’t been updated for your infrequently updated transactional messages. Let’s use inline AMPscript to update a copyright line for your email footers. This simulation shows a specific date, but this AMPscript string always shows the current date when used in your content.
- Click Next Exercise and then Begin.
- Click to return to the Content Builder screen.
- Under Local Folders, click Simulator.
- Click Create, select Content Blocks, and then select Free Form.
- Click inside the body of the Content Window and enter
Copyright:
and today’s year in yyyy format, then press Enter. - On the next line, enter:
Copyright:
%%=Now()=%%
and Enter. - On the next line, enter:
Copyright:
%%=Format(Now(), "yyyy")=%%
and Enter. - Click Save , and then click Save and Exit .
- In the Name field, enter
Copyright Year Sample
, then press Enter. - Click Save.
- From the list of emails, click Cloud Kicks Loyalty.
- Click Edit and select Edit Content.
- Click the Content tab.
- From the Content section, drag Copyright Year Sample to the bottom of the Content Block and click Done Editing .
- Select Preview and Test to view your results.
- Click Save.
Never have an outdated copyright date again!
Exercise 3:
In the next scenario, we focus on creating a personalized image based on a customer’s preference, in this case for their preferred sport. Let’s walk through how to use variables and the lookup function to load and display content stored in a data extension.
- Click Next Exercise and then Begin.
- From the list of data extensions, click Exercise_Image.
- Review the info on the Properties tab and notice the names under Fields.
- Click the Records tab and review the records to see how the data maps between Hero_Image and PrefSport.
- Now, navigate to the Content tab, and then click the Simulator folder.
- Click the template-based email CloudKicks Preferred Sport.
- Click Edit and select Edit Content.
- In the email, hover over the Code Snippet section of the email, and then click once to edit.
- In the Code Snippet window, select the portion of code that looks up data from the data extension, then click Done Editing.
- Next, click Preview and Test.
- Click to view the list of recipients.
- Click Data Extensions, the Simulator folder, and then click Sample_AMPscript.
- Click the entry with MemberID 345678, FirstName Tyler and then click Select.
- Review the recipient information and notice:
- The name in the email salutation is Tyler, derived from the FirstName value.
- The image is the running image and corresponds to the Prefsport value of running.
- Click to view the next recipient.
- Review the recipient information and notice:
- The name in the email salutation is brienne (notice the first name in the data extension isn't capitalized), derived from the FirstName value.
- The image is the horseback riding image and corresponds to the Prefsport value of horseback riding.
- Click to view the third recipient.
- Review the recipient information and notice:
- The name in the email salutation is Jamie, derived from the FirstName value.
- The image is of weight lifting and corresponds to the Prefsport value of weight lifting.
- Click Save.
- To stay in the Simulator and retry the exercises, click Home. Otherwise, click Exit to exit the Simulator.
The images for the three recipients change because the lookup code Lookup("Exercise_Image","Hero_Image","PrefSport",@PrefSport)
, finds a specific image to be displayed for a user based on the data extension identified (hint: the Hero_Image and PrefSport fields we previously reviewed).
Congratulations! Even if you don't feel fluent in AMPscript quite yet, you now have the basics to go out on your own and explore. Experiment with AMPscript in your email communications and then if you are inspired to learn more, delve into our developer documentation to gain even further knowledge. Happy AMPscripting!