Velocity script to change greeting based on timezone

Velocity script to change greeting based on timezone


We'd like to change the greeting of the email to be "good afternoon" or "good morning" based on the recipients timezone. Has anyone done this before and can guide me in the right direction?

Thank you!


Not applicable

Re: Velocity script to change greeting based on timezone

Hi, Catherine - I may have a simpler solution than Velocity Script for you. Depending on whether or not the deployment times of your emails are consistent, you could use a concatenated string field (formula field). Check them out here: Create and Use a Concatenated String (Formula) Field - Marketo Docs - Product Docs

The formula field isn't really designed for the timezone use case, but could potentially provide a nice workaround.

As for velocity script, unfortunately, I don't have a quick solution for you there. It's definitely possible. If you're comfortable with coding, it may be worth exploring stack overflow for solutions in other languages, to help translate the general structure to velocity script.



Re: Velocity script to change greeting based on timezone

Sure, straightforward enough:

  • Get now() as a date in the system timezone
  • Convert to lead timezone (I'm assuming, of course, you have this stored in the lead in proper tz format like America/Los_Angeles)
  • Extract hour and compare to your morning/afternoon/evening ranges

I'll drop some code later -- have to go out for a bit.

Re: Velocity script to change greeting based on timezone

Hi Sanford, that would be amazing, thank you!!

Re: Velocity script to change greeting based on timezone

Hi Catherine,

You might need a bit of a reality check here. You might have missed it, but Velocity is server-side and it's not like Javascript where the processing is done client side.

In other words, your lead record has to include a field that can tell where your lead is located in the world.

I'm going to guess that you don't have this info. Yeh, you could reverse engineer this info but I can think of better things to do than figure out what timezone various cities are.

It also means that the Good evening / Good morning is written when the email is sent, not when the email is opened. You're assuming your customers will be reading their emails in a short timeframe after the email is sent.

Re: Velocity script to change greeting based on timezone

That's definitely all true, Nicholas, and like I said above I make the assumption that the lead's home timezone string is known. Otherwise the whole idea is a non-starter!

But I still think this is a nifty bit of personalization. Because, let's face it, rarely do people do enough A/B testing to really determine "optimal" send times for their leads, if there even is one (let alone the delta between send and open time). 

This way, you can send the batch at, say, your 12pm, but show the lead that you know what time it is there. If they open the email promptly, this may -- I stress "may," as this too should be A/Bed -- be a nice touch. It's a way of smoothing over the lack of true business intelligence and might help... can't imagine how it would hurt!

Re: Velocity script to change greeting based on timezone

#set( $lead = {

  "tzstring" : "America/Los_Angeles"

} )


#set( $dateOptions = {

  "timezones" : {

    "userout" : $lead.tzstring


  "dayParts" : [


      "name" : "Wee Hours",

      "min" : 00,

      "max" : 06



      "name" : "Morning",

      "min" : 07,

      "max" : 11



      "name" : "Afternoon",

      "min" : 12,

      "max" : 17



      "name" : "Early Evening",

      "min" : 18,

      "max" : 19



      "name" : "Night",

      "min" : 20,

      "max" : 21



      "name" : "Late Night",

      "min" : 22,

      "max" : 23



} )

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

#set( $tmp = $dtHere.setTimeZone( $date.getTimeZone().getTimeZone($dateOptions.timezones.userout) ) )

#set( $hourThere = $dtHere.get($$dtHere).HOUR_OF_DAY) )

#foreach( $part in $dateOptions.dayParts )

#if( $part.min <= $hourThere && $hourThere <= $part.max )

#set( $dayPart = $ )



Good ${dayPart}, Lead-O!

As Nicholas and I both noted, you need to have the lead's timezone name on the lead. That's "America/New_York", "America/Los_Angeles", etc. or (not recommended) the static daylight-aware offsets like "EST5EDT".  Note that "EST" is not a timezone you should be storing on a lead, and "PST" isn't a valid timezone at all. This is a frequent and aggravating mistake that people make (and which they're encouraged to make by bad tutorials). You need to use a zone that is daylight-aware. If you say someone is in EST, that means they're in EST all year round. Not good!

In the code above, the timezone is stored in the $lead.tzstring field -- obviously change the name to whatever field you're using after checking it off on the right-hand-side of Script Editor.

The dayParts array should be self-explanatory: set up stop and start hours (00-23) for each named part of the day. You may want to have a coarser approach with just morning/afternoon/night, or you can divide it as finely as I did here.

Then you'll have the $dayPart variable that matches the name of the part they're in. You can use that to switch the greeting. (I would not hard-code the greeting in the dayParts. Use generic names there, then you can do

#if( $dayPart == "Night" )

It's spooky out tonight, isn't it?