Using Temporary Objects with Multiple Scripts in your Marketo Emails

garyvolland
Marketo Employee
Marketo Employee

The last several email scripting projects I’ve worked on have proved to be good use cases for a Velocity Script’s powerful feature, the use of temporary objects with multiple email script tokens. This feature allows marketers to efficiently manage and manipulate data, enabling them to create more engaging and personalized email campaigns. In this blog post, I’ll explore these features in more depth, discussing their benefits and best practices for implementation.

 

Temporary Objects in Marketo Velocity Script

 

Temporary objects in Velocity Script can be an array or structured object.  These objects are particularly useful to store a subset of records from a custom object.  Temporary objects can also be populated by evaluating JSON stored in a field or from a custom object record.  ETL processes can be used to transform the data before storing it in the object.

 

 

Benefits of Using Temporary Objects

 

  • Efficient Data Manipulation: Temporary objects allow you to manipulate data efficiently within the context of an email send, without the need to modify the underlying data source.
  • Improved Performance: By performing calculations and data manipulations using temporary objects, you can improve the performance of your email campaigns by reducing the complexity of your Velocity scripts.
  • Enhanced Personalization*: Temporary objects enable you to create more personalized and targeted email content by allowing you to customize the content based on user-specific data.

 

Best Practices for Temporary Object

 

  • Use Descriptive Names: Use descriptive names for your temporary objects to make your Velocity scripts more readable and easier to maintain.
  • Avoid Overuse: While temporary objects are powerful, overusing them can lead to complex and difficult-to-maintain Velocity Scripts. Use them judiciously for maximum effectiveness.

 

Using Temporary Objects with Multiple Email Script

 

In a recent use case, my customer wanted to send birthday greetings to pet owners on their birthday.  The pet information is stored in a custom object.  An owner may have one or more pets and the custom object holds pet information for current and past pets. 

 

There were multiple sections on the email that was personalized.  For example, the hero banner is different a cat or dog.  The pet’s name(s) were used in different sections.  For owners with multiple pets, the names were required to be listed, ie.  Happy Birthday Spot and Rover.

 

The first token, PetBirthdays, is used get all the records for living pets whose birthday was today. 

 

 

##PetBirthday

#set( $pets = [] )

#set( $cal = $date.getCalendar() )

#set( $ret = $cal.add(5,-1) )

#set( $today = $date.format('MMdd', $cal) )

#foreach( $pet in $pet_cList )

#set( $petBDay = $date.format('MMdd',$convert.parseDate($pet.dateOfBirth,'yyyy-MM-dd')) )

#if( $petBDay == $today && ! $pet.deceased == true  )

#set( $ret = $pets.add($pet) )

#end

#end

 

 

These records were added to an array $pets using add function.  #set( $ret = $pets.add($pet) ).   Note: the set directive is used to call the add function.  The variable, $ret is only used to record the response. 

 

In the right navigation, the fields used in the additional scripts are selected here.  This is not required in the other scripts.

 

The pets with birthdays have been determined and added to the array. 

This token is added to be beginning of the email.  A great place to put the first token is in the From of the email editor.

 

The rest of the script tokens can be written to use these records by referencing the $pets array.

 

In this example, the pet names are combined depending on the number in the array.

 

 

##Pet Birthday Names

#set( $namepet = "your pets" )

#set( $listSize = $pets.size() )

#if( $listSize == 1 )

#set( $namepet = $pets[0].name )

#elseif( $listSize == 2 )

#set( $namepet = "${pets[0].name} and ${pets[1].name}" )

#elseif( $listSize == 3 )

#set( $namepet = "${pets[0].name}, ${pets[1].name} and ${pets[2].name}" )

#end

${namepet}

 

 

The script is simplified because the number of pet records have already been determined.  The records do not need to be extracted from the custom object again.

 

Best Practices for Using Multiple Email Script Tokens

 

  • Plan Your Tokens: Before creating your email templates, plan out the tokens you'll need and the data they'll represent to ensure a smooth implementation.
  • Test Thoroughly: Test your email templates with different sets of data to ensure that the tokens are being replaced correctly and that the email content is rendering as expected.

 

In conclusion, Marketo's Velocity scripting language offers marketers a powerful tool for creating dynamic and personalized email campaigns. By using temporary objects and multiple email script tokens, marketers can efficiently manage and manipulate data, leading to more engaging and effective email campaigns. By following best practices for implementation, marketers can maximize the impact of their Velocity scripts and drive better results from their email marketing efforts.

1043
0