While you are running a Split URL test on your website, there could be scenarios where you may want VWO to execute a specific portion of the code based on whether the user is redirected to a variation or not. For example, you may want to execute a code snippet only when there is no redirection or want to avoid GA Code execution on the control if it redirects to another variation.
In this article, we will talk about the following use cases:
- Executing a code snippet only when redirection is not happening
- Avoiding GA Code execution if the page is going to be redirected to variation
- Using VWO with Exit Pop-up Code
Executing Code Only When Redirection Is Not Happening
If you only need to execute code when the redirection does not occur, then you can attach an event to listen for redirection. You can do this by adding the following code snippet before VWO SmartCode (let’s call it Code-1).
Avoiding GA Code Execution if the Page Is Going to Be Redirected to Variation
If a split campaign is running and GA is installed, GA will log a visitor for the control page and the variation page (for both Synchronous and Asynchronous code). To avoid this, install code-1 above VWO Sync Code and then execute the GA tracking code conditionally.
For asynchronous SmartCode, this implementation will not work because the VWO library doesn't need to be loaded by the time the script executes. For asynchronous code implementation, add the following code:
- Remove the following line from the code:
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ABCD1EFG2H"></script>
- Replace the following code with your GTM tracking code:
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-ABCD1EFG2H');
Using VWO with Exit Pop-Up Code
You can also use VWO Sync Code with websites with exit pop-ups. Add the Code-1 snippet and then the following code before VWO Sync SmartCode: