Traffic source specific A/B testing for inpage widgets
- Add Trustmary tag script to your website head tag or with Google Tag Manager
- Pick and customize one of the examples below and add it to your preferred location in your website.
Example 1: Show experiment with id "EXP1234567" only if utm_campaign query parameter equals august_newsletter
<div id="tm-conditional-1"></div>
<div id="tm-conditional-1"></div>
<script>
let wrapperId = `tm-conditional-1`;
function getEmbedCode() {
if (window.location.search.includes(utm_campaign=august_newsletter))
return `<div data-trustmary-experiment="EXP1234567</div>`;
else return false;
}
let embedCode = getEmbedCode();
let wrapperElement = document.getElementById(wrapperId);
if (embedCode && wrapperElement) {
wrapperElement.innerHTML = embedCode;
if (window.TrustmaryEmbed) {
window.TrustmaryEmbed.initAllExperiments();
window.TrustmaryEmbed.initAllWidgets();
}
}
</script>
For the rest of the examples use the same code, but replace getEmbedCode function with the provided example.
Example 2: Same as Example 1, but show widget with id "DEFAULT123" to visitors who are not part of the august_newsletter utm_campaign.
function getEmbedCode() {
if (window.location.search.includes(utm_campaign=august_newsletter))
return `<div data-trustmary-experiment="EXP1234567"></div>`;
else return `<div data-trustmary-widget="DEFAULT123"></div>`;
}
Example 3: Show widget with id "WIDGET1234" to visitors whose referrer includes "google.com" and widget with id "DEFAULT123" for other visitors
function getEmbedCode() {
if (document.referrer.includes(google.com))
return `<div data-trustmary-widget="WIDGET1234"></div>`;
else return `<div data-trustmary-widget="DEFAULT123"</div>`;
}
You can conditionally display embeds in different parts of your website by repeating the above process with different wrapperIds: tm-conditional-2, tm-conditional-3, etc.
Updated on: 28/02/2025
Thank you!