javascriptcontent-experiments

How to write a Google Content Experiment that disables a stylesheet?


I am currently trying to setup a Client Side Experiment without Redirects as opposed to the default Content Experiment A/B testing. All I want to do is for half my visitors disable one of two existing stylesheets on a page.

To me it doesn't make sense to do this the default A/B way because then I would have to setup a second page with the stylesheet disabled for every page on my site.

There is also Running a Server-side Experiment but to me that is too heavy handed for something I think should be somewhat simple.

I have the js all setup, I just need to be able to tell the page that when there is a given variation disable the stylesheet or don't render the stylesheet before the DOM loads.

One thing I considered is, given a certain variation, redirect to the same page but append a url query parameter like &stylesheet_disabled=true then I would simply not render the stylesheet on the server side but when I briefly looked at that I ran into a redirect loop but perhaps someone has a better way to write the js.

Any help greatly appreciated.


Solution

  • You could do something like this:

    <head>
      ...
      <script>
        if (cxApi.chooseVariation() != 1) { // not in the experiment
          document.write('<link href="... />'); // add the stylesheet
        }
      </script>
      ...
    

    Only use document.write while the page is still being loaded.

    This is all client-side, so no need for redirects and extra server work.