By - Elise Maile

Tips and Tricks for Coding Experiments For CRO

Image of javascript for coding experiments
Photo by Pankaj Patel on Unsplash

A Guide for front-end developers on coding experiments.

 

I’m a self-taught developer since the age of 15. I’ve spent 10 years of my career working as a freelance web designer and developer.  I fell into coding experiments for CRO and A/B testing, and made Javascript my bitch.

 

But, when I started coding experiments, I found it to be an unusual mix of the familiar and the new. I want to share some of the quirks about coding for A/B tests today.

 

1. Disposable code:

 

The first thing I realised about coding experiments was whatever code I wrote, it was likely to be thrown away after the test ended. There are a couple reasons for this:

 

  • The experiment can (and often will) fail. A failed test is still a learning experience, but as a developer, it can be disheartening to realise that the complex trigger you spent a day building is irrelevant.
  • If building a client-side experiment in a third party tool, the code you’re writing is manipulating the DOM and so unlikely to be useful for the engineers. Manipulation involves using Javascript to modify the page, which is just not how websites are built in the source. I’ve had my code called ‘hacky’ which hurt, but was technically true — I’m hacking the DOM to make a change. 

 

It can be difficult to accept that all that time spent struggling to target a certain element, or getting the experiment to trigger at the right moment, was a waste of effort. But the whole point of experimentation is to be a learning experience; even the failures teach us something, and your disposable code is vital to that process. 

 

Coding experiments is disposable
Your code is disposable. (Photo by Steve Johnson on Unsplash)

 

2. Limitations:

 

There are always going to be limitations when building an experiment, but there are plenty of workarounds to get the required results. Firstly, don’t use the WYSIWYG (what you see is what you get) tool provided by third party tools. It’s usually rubbish and inserts untidy and unreliable JQuery. 99% of experimentation tools have a developer mode which allows you to insert CSS and javascript written by you, the expert.

 

Secondly, if you’re running client-side testing (as opposed to server-side testing) then you will be limited to modifying what is already present in the DOM upon load. You will have to learn to work within this limitation, for example, creating new blocks of HTML or editing copy or layouts. 

 

However, if a change is to be generated dynamically, then that content needs to be exposed via a data layer. For example, moving pricing or availability of a product to an earlier stage in a funnel. Without a data layer exposing this information on the page it’s required, you won’t be able to build the experience. Work with your engineers to ensure a data layer is functional for testing. Ideally this should be done early on when installing your experimentation tool, so that dynamic content can be pulled throughout the site. 

 

3. Targeting and triggering 

 

URL targeting of experiments in Google Optimize
URL targeting in Google Optimize

 

Making sure the experiment fires at the right time is likely to be the hardest part of a test build. There are multiple moving parts to consider, but the most common is to target the URL. This can be precise using “equals” or “exact” which will not allow parameters or trailing slashes. Alternatively, you can target URL “contains” or “substring” which targets a URL based on whether it contains a set of characters that can be present anywhere in the URL. This is useful for targeting pages based on parameters (ie referral traffic from social or email.) Some tools give you the option to use regex to target URLs as well. 

 

You are not restricted to URL for targeting though. Sometimes you may need to use information available in the data layer to ensure your experience is firing for specific products only (relevant if URL structure is unrelated to product type). Or you may need to search for a unique element on the page, whether a cookie is or is not present (based on previous exposure to an experiment for example) and, of course, audience segments (new vs returning, referral, device type etc). 

 

Tests can also be triggered based on a user’s action — (e.g. clicking on an element, or having a certain threshold of items in their basket for example). Again, most testing tools offer a way to ensure these triggers are met, sometimes this is via audience segmentation/audience behaviour selectors, or a custom Javascript trigger section. Check your documentation for the solution offered. 

4. QA Experiments:

 

You cannot QA your experiment enough. Make sure you test your experiment throughout the build. Check it on the devices it is designed to fire on, try going forward and backward through the user journey. Run through the whole user journey, try jumping around to unexpected pages — not all users follow the funnel laid out for them. And make sure you have a peer QA it as well. 

 

Use Ghostry, or an alternative third party tracker tool. What I love about it is that if something looks broken on a website, but I’m not sure if it’s an A/B test causing the problem, I can use Ghostry to simply stop the testing tool from loading. If the bug still appears, then it’s not the experiment. I recommend this browser add-on to all product owners, engineers and CRO specialists as it prevents A/B tests getting the blame for every UI problem that appears on a website, when 9 times out of 10, it’s not the experiment that’s the problem. 

 

But, a word of warning about coding experiments. Sometimes there will be something called an ‘edge case bug’ where the experiment does not fire when it should, or maybe it looks a little crooked going from one screen size to another. It is vital to weigh up the effort of fixing that bug vs the chance that a user might see it. 

 

This is where the analytics team are your friends. Get them to check the number of users on that screen size, or how many go from page A to page H and back again. If the impact on users will be very low, but the effort to fix the bug high, it’s often best to simply run the test. Remember, it is temporary and will be turned off and your code disposed of in the space of a few weeks, the learnings will be greater than the risk of an edge case bug. 

 

After coding experiments remember to QA
Remember to QA your experiments. (Photo by Cookie the Pom on Unsplash)

5. Goals:

 

Goals / KPIs should have been established early within the experiment hypothesis. Do not forget to add them, and confirm that the tracking is working either via the testing tool itself or the analytics solution. Adding the goals is part of building a test, so it should be part of your checklist, but if you think something should be tracked that has not been included in the spec, ask, because once a test is live, data tracking cannot be retrofitted. 

 

Goals are usually a form of event tracking, such as clicks on elements or visits to certain pages. Depending on the tool being used, you may be able to use the WYSIWYG to select the element to track, or you may need to code this in directly, or even use a tool such as Google Tag Manager to create the event tag & trigger. Check your experimentation tool documentation for instructions.

 

element.addEventListener(“click”, fnClickTrack);

function fnClickTrack() {

console.log (“Add the tracking code for your tool here”);

}

Goals can also be revenue related such as average order value, or engagement rates like bounce or session time. 

 

6. Miscellaneous: 

 

If your company isn’t set up for server-side testing, you can still use experimentation tools to switch features that have been coded in the main code base on and off. This is great for testing new features, larger changes such as element redesigns or if the experimentation tool itself is clunky for a developer to use (Google Optimize I’m looking at you). Expose both control and the variation code to the DOM, then use CSS or Javascript to show & hide it based on whether the user is bucketed into the variant or not.

 

if(window.lp__test) {

console.log(‘Experiment loaded – activate’);

lp__test.init();

} else {

console.log(‘Experiment not loaded yet – sett flag’);

lp__experiment_flag_test = true;

}

 

Saying all that, using an experimentation tool to test a page redesign is not the best use of A/B testing because it is difficult to ascertain which element caused the impact. You should encourage CRO specialists to use qualitative measures, such as user testing to validate design decisions.

7. Create a code library:

 

Finally, if you spend any time coding experiments, you’ll quickly realise the amount of copy/paste you’ll do. I highly recommend keeping a library of code snippets for quick access for commonly used commands. For example, onClick tracking, setting and retrieving cookies, and checking URL parameters contains. These  are snippets I use enough to not know them off by heart, but to find them easier to copy/paste from my code library. You can use Github to create a code snippet library, or any note taking tool that suits you. 

 

Conclusion:

 

Whether you’re new to coding experiments or an experienced optimisation developer, I hope these tips were useful. There are a lot of points to consider when coding CRO experiments, and it can be frustrating when someone asks you to “just” make this change when they don’t understand the complexities involved in manipulating the DOM and QA-ing the work involved.

 

Remember to communicate clearly with your team, especially when blockers occur in the experiment coding process, and use these tips to know when to push back and ask for clarification about the work you’ve been asked to do.

Image of Elise Maile

 

Elise Maile has 15 years experience within the Digital sector including 6 years in UX, personalisation and optimisation testing. Having worked in a variety of industries including travel, auctions, e-commerce and the charity sector.

She enjoys the fast paced test and learn cycle, analysing data and problem solving, but also utilising her well-honed design and development skills. She is a frequent attendee to CRO events, but her main goal now is to share her knowledge, something she does through writing articles, conducting workshops and mentoring.

1 thought on “Tips and Tricks for Coding Experiments For CRO

Rob Kingston 9th June 2020 at 1:10 am

Keeping a code library is pretty underrated for anyone building experiments by hand. We sometimes resurface code from up to 12 months ago and it saves hours – particularly when your old solution has already been code reviewed/QA’d and tested in prod.

Over the years we’ve also learned to:

1. Use git to track your changes amongst your team
Rather than copy/pasting back and forth into an editor, we maintain a git repository. It facilitates code review and acts as a source-controlled code library you can refer to.

When you’re not reliant on SaaS split testing library, you can even build, lint, minify & launch experiments right from VS Code. We use (and maintain) the Mojito split testing library to achieve this.

2. Track & handle errors
You can’t test your code in all different browser configurations, and no QA’er will test control/treatment builds across ALL pages/flows. We tend to release experiments to canary, track errors using try/catch and handle errors so that the user experience is protected from any issues introduced by your experiment.

Keeping experiments error free helps you avoid SRM issues, too: https://www.lukasvermeer.nl/srm/

Comments are closed.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close