javascriptwordpressgoogle-optimize

Google Optimize: How to A/B test, with and without a Javascript script


I've the following script in my website:

<script type="text/javascript" src="https://code.evidence.io/js/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjE2OTh9.2mZpV00Ls3Wgrl7Knm-Bh2z-I7lPaX_5b4d-RbuWl90"></script>

I don't find a way to "delete" it in Google Optimize: is there anyway to see the source code, select this script and "delete" it for the test? (I only see ways to select elements with ID or class, but not scripts).

If not, my best idea is deleting that script from my website and add it to the Google Optimize Global Javascript section.

But I've no idea about how to put that script within that function(){ }

Not sure if that idea has sense actually.

Actually, I see a problem with that idea: I don't find how to add that Javascript code to the whole website as it seems prepared to test single pages.

Screenshot: Google Optimize Global Javascript section

Note for future people searching for a solution of question: I've also asked the same question here: https://support.google.com/optimize/thread/84843254?hl=en


Solution

  • So a "community specialist" gave me the solution. I hope summarising it here can help somebody else:

    If you want to delete a element in the page, that's not possible. The script has already been executed, removing the element does not do anything.

    The workaround or way to do it is deleting the script in the website.

    Then add to Global Javascript (on a Google Optimize variant) the following code to inject the script:

    const scriptEl = document.createElement('script');
    scriptEl.src = 'https://.....';
    document.head.appendChild(scriptEl);
    

    That's working perfectly for me at this time.

    Ah, I've not found a way to test the script on the whole website, so I've to create several experiments instead.

    --UPDATE--

    Script to load the Javascript script everywhere EXCEPT on the Google Optimize pages where we are running the experiment.

    In my specific case I've realised the above solution is not enough because I need the script on the whole website to capture the daily number of users on the website.

    So have added the following to the head of the website globally:

    <!-- Load Javascript script globally except on the page where we are testing it with Google Optimize -->
    
    <script>
    
      if(window.location.href.indexOf("/URL-OR-PART-OF-THE-URL-1/") > -1) {
        // (If the URL contains the above, load nothing)
      }
      else if(window.location.href.indexOf("/URL-OR-PART-OF-THE-URL-/") > -1) {
        // (If the URL contains the above, load nothing)  
      }
      else { 
        // Loads Javascript Evidence pixel    
        (function () {
        var params = {}; // If you want to pass anything, to the called function
        var script = document.createElement("script");
        script.type = "text/javascript";
        if (script.readyState) {
            script.onreadystatechange = function () {
                if (script.readyState == "loaded" || script.readyState == "complete") {
                    script.onreadystatechange = null;
                    theFunctionInsideExternalScript(params);
                }
            }
        } else {
            script.onload = function () {
                theFunctionInsideExternalScript(params);
            }
        }
        script.src = "https://SCRIPT-URL-TO-LOAD-GOES-HERE";
        document.getElementsByTagName("head")[0].appendChild(script)
        })();
      }
    
    </script>