Forum Discussion
Hey SofiaZ 🙂 I would suggest to use Liquid as well, but instead of storage the 10 different text versions in an if function, you can create a content block with an JSON object and use a content block 😉. Let me explain how to do it:
First we have to create a JSON object like:
{
"subjectline":{
"0":{
"text":"This is a great subject line"
},
"1":{
"text":"This is another great subject line"
}
}
}
Continue this schema with your 10 different versions. Important: You have to start with 0 and please format the JSON object with a formatter.
Next, we create and save a content block and add some liquid, like this:
{% capture subjectElements %}
{
"subjectline":{
"0":{
"text":"This is a great subject line"
},
"1":{
"text":"This is another great subject line"
}
}
}
{% endcapture %}
{% assign object=subjectElements | json_parse %}
Now, we just have to add the following liquid in our communication to create a random number and select a random copy. The liquid below will generate a random number between 0-9 :
{% assign timeinsec = 'now' | date: "%s" %}
{% assign randomNumber = timeinsec | modulo: 10 %}
Now, we have to use this number to generate our random subject line with:
{{content_blocks.${random_text}}}
{% assign timeinsec = 'now' | date: "%s" %}
{% assign randomNumber = timeinsec | modulo: 10 | append: randomNumber %}
{{object.subjectline[randomNumber].text}}
Short explanation:
- We add the content block, which we created
- We create a variable to define the current time in seconds
- We create a random number based on the time variable and modulo it by 10. Furthermore, we also add the string filter 'append' to change the variable from an integer to a string.
- We use our object with the random number to display the random text
What is the benefit behind this solution?
- The content in your communication is shorter and more clearly arranged
- You can always make changes easily in the content block
Let me know if this worked for you, or you have any questions!
Cheers!
Related Content
- 2 years ago
- 7 months ago
- 2 years ago
- 6 months ago