Forum Discussion
Randomize or changing copy each time customer enters the same canvas
Hi SofiaZ
I would use a content block of Liquid as jakedavis suggested but rather than a custom attribute you could place a random number generator at the start so that every time the block is run it chooses a random number and lines of text.
{% assign randomNumber = "now" | date: "%N" | modulo: 10 %}
{% if randomNumber == '0' %}
Subject Line 1
{% elsif randomNumber == '1' %}
Subject Line 2
{% elsif randomNumber == '2' %}
And on and on
{% else %}
Subject Line for the null value on first entry
{% endif %}
Changing the 'modulo' number will change the range of random numbers. In this case it is 0 - 10. If you change it to module: 25, it will select between 0 - 25.
Technical logic:
randomNumber is taking the exact time of day 'now' and converting it to %N which is 'Fractional seconds digits, default is 9 digits (nanosecond)', which updates all the time.
It comes back with numbers such as:
928616331
201621187
187114708
This number is then divided by the value of modulo and only the remainder of that equation is returned which becomes your new randomNumber.
modulo – Liquid template language (shopify.github.io)
Note: This random number generator does not remember what was picked last time so it is possible that it will produce the same randomNumber twice in a row. Otherwise, it would need to be more complex and include an array of previously selected numbers.
😊
Thank you so much David, and also for explaining how Modulo works. I've seen peopel reference it often but can never understand how it works.
Related Content
- 2 years ago
- 8 months ago
- 2 years ago
- 7 months ago