SOLVED

Lightweight A/B testing using Velocity

Go to solution
Jon_Wright
Level 2

Lightweight A/B testing using Velocity

Hi @SanfordWhiteman  I've been looking to use this

https://blog.teknkl.com/lightweight-a-b-testing-in-velocity/

 

but also with different (tracked) links. I'm aware of the multiple tracked links issue but I though renaming the variable would work e.g.

 

 

#set( $random = $math.random(0,3) )
#set( $member_cta = {
0 : {"text": "Text 0",
"link_text": "Link text 0",
"url": "www.example.com?l=0"
},
1 : {"text": "Text 1",
"link_text": "Link text 1",
"url": "www.example.com?l=1"
},
2 : {"text": "Text 2",
"link_text": "Link text 2",
"url": "www.example.com?l=2"
}
})
#set($url_new = $member_cta[$random].url)
${member_cta[$random].text}
<a href="https://${url_new}" style="color: #ffffff;">
${member_cta[$random].link_text}</a>
${url_new}
</div>

 

 

And indeed it was, but turns out it was only working 'randomly'. I only want to output 1 link, what am I doing wrong here?

Thanks
Jon

1 ACCEPTED SOLUTION

Accepted Solutions
Jon_Wright
Level 2

Re: Lightweight A/B testing using Velocity

Thanks for confirming. Saves me tearing any more hair out.

View solution in original post

8 REPLIES 8
SanfordWhiteman
Level 10 - Community Moderator

Re: Lightweight A/B testing using Velocity

Hi Jon,

 

I tried responding to your comment on my blog, but by then it was deleted!

 

The problem is that your options HashMaps are still ultimately properties of the same $member_cta object.  (Well, if we looked at them in Java terms, they're not owned by the $member_cta in any way, but from the perspective of the Velocity UberSpector, the fact that they're referenced as properties of the HashMap makes them seem like "children" of the HashMap.)

 

You would have to create entirely different top-level objects.

Jon_Wright
Level 2

Re: Lightweight A/B testing using Velocity

Thanks @SanfordWhiteman . I did post there but couldn't get the script to display in the comments so thought better to post over here instead.

 

So I've tried this (thinking I was creating a new object) but still no joy. 

#set( $random = $math.random(0,3) )
#set( $member_cta = {
  0 : {"text": "Text 0",
       "link_text": "Link text 0",
  	   "url": "www.tmforum.org/toolkits-overview/?l=0",
       "campaign": "member-footer"
        },
  1 : {"text": "Text 1",
        "link_text": "Link text 1",
  		"url": "www.tmforum.org/toolkits-overview/?l=1",
        "campaign": "member-footer"
        },
  2 : {"text": "Text 2",
        "link_text": "Link text 2",
  		"url": "www.tmforum.org/toolkits-overview/?l=2",
        "campaign": "member-footer"
        }
})
#set($cta = $member_cta[$random])
#set($new = {})
#set($void = $new.put("u", $cta) )
#set($new_url = $new["u"].url)
<div><b>${new["u"].text}</b> 
<a href="https://${new_url}" style="color: #ffffff;">
${new["u"].link_text}</a>
$new
</div>

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Lightweight A/B testing using Velocity

Markdown works on my blog, FYI.

 

This still isn't a new object because it's just shuttling the object reference around.

Jon_Wright
Level 2

Re: Lightweight A/B testing using Velocity

@SanfordWhiteman thanks, missed the markdown option.

 

I'll admit I'm a bit stumped how I can create a new object from the current object without referencing the current object. Just always seems I'm shifting the reference around. I tried mixing up lots of code snippets to get a 'map-y string' and converting back to a map, but that didn't work either i.e.: 

 

#set( $member_cta = {
  1 : {"text": "Members get more: ",
       "link_text": "Text 1",
  	   "url": "www.tmforum.org/toolkits-overview/?l=1",
       "campaign": "member-footer"
        },
  2 : {"text": "Text 2",
        "link_text": "Link text 2",
  		"url": "www.tmforum.org/toolkits-overview/?l=2",
        "campaign": "member-footer"
        },
  3 : {"text": "Text 3",
        "link_text": "Link text 3",
  		"url": "www.tmforum.org/toolkits-overview/?l=3",
        "campaign": "member-footer"
        }
})

#foreach( $key in $member_cta )
  #set( $downloadLink = $member_cta[$foreach.count] )
  #evaluate( "${esc.h}set( ${esc.d}downloadLink_${foreach.count} = ${esc.d}downloadLink )" )
#end

#set( $random = $math.random(1,3) )
#set($dynamicsource = "$downloadLink_$random")

#set( $new_map = '#set( $new_map = ' + $dynamicsource + ' )' )
#evaluate( $new_map )
#set($url = $new_map.url)

<div><b>${new_map.text}</b> 
<a href="https://${url}" style="color: #ffffff;">
${new_map.link_text}</a>
</div>

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Lightweight A/B testing using Velocity

By which I mean

 

#set( $link1 = {
  "href" : "www.example.com/offer1",
  "text" : "Offer 1"
} )
#set( $link2 = {
  "href" : "www.example.com/offer2",
  "text" : "Offer 2"
} )
#set( $link3 = {
  "href" : "www.example.com/offer3",
  "text" : "Offer 3"
} )

 

and then

 

#set( $random = $math.random(1,3) )
#if( $random.equals(1) )
<a href="https://${link1.href}">${link1.text}</a>
#elseif( $random.equals(2) )
<a href="https://${link2.href}">${link2.text}</a>
#elseif( $random.equals(3) )
<a href="https://${link3.href}">${link3.text}</a>
#end

Clunky but separates the references.

 

Jon_Wright
Level 2

Re: Lightweight A/B testing using Velocity

thanks @SanfordWhiteman only just picked this up. I was kind of hoping to avoid an if  statement as potentially could have up to 20 different links

SanfordWhiteman
Level 10 - Community Moderator

Re: Lightweight A/B testing using Velocity

It's just not possible because of the way the UberSpector works.
Jon_Wright
Level 2

Re: Lightweight A/B testing using Velocity

Thanks for confirming. Saves me tearing any more hair out.

View solution in original post