Why doesn't Google Optimize javascript execute on page refresh?












0















I'm using Google Optimize to run an A/B experiment on a page, by injecting a line of Javascript:



$(document).ready(function(){console.log(1);});


When I run the experiment and view the targeted page in a new incognito window, "1" is successfully logged to the console.



But if I refresh the page, nothing is logged to the console. I can navigate to and from the page, but nothing is ever logged.



I can only get it to work again if I close my incognito session and start a new one.



It's as if Optimize is only executing the script the first time the variation is seen by a session user?



How can I get the JS to run every time the page is viewed in a session?



Note: I have tried placing the code in the body and the head, both after opening tag and after closing tag.










share|improve this question



























    0















    I'm using Google Optimize to run an A/B experiment on a page, by injecting a line of Javascript:



    $(document).ready(function(){console.log(1);});


    When I run the experiment and view the targeted page in a new incognito window, "1" is successfully logged to the console.



    But if I refresh the page, nothing is logged to the console. I can navigate to and from the page, but nothing is ever logged.



    I can only get it to work again if I close my incognito session and start a new one.



    It's as if Optimize is only executing the script the first time the variation is seen by a session user?



    How can I get the JS to run every time the page is viewed in a session?



    Note: I have tried placing the code in the body and the head, both after opening tag and after closing tag.










    share|improve this question

























      0












      0








      0








      I'm using Google Optimize to run an A/B experiment on a page, by injecting a line of Javascript:



      $(document).ready(function(){console.log(1);});


      When I run the experiment and view the targeted page in a new incognito window, "1" is successfully logged to the console.



      But if I refresh the page, nothing is logged to the console. I can navigate to and from the page, but nothing is ever logged.



      I can only get it to work again if I close my incognito session and start a new one.



      It's as if Optimize is only executing the script the first time the variation is seen by a session user?



      How can I get the JS to run every time the page is viewed in a session?



      Note: I have tried placing the code in the body and the head, both after opening tag and after closing tag.










      share|improve this question














      I'm using Google Optimize to run an A/B experiment on a page, by injecting a line of Javascript:



      $(document).ready(function(){console.log(1);});


      When I run the experiment and view the targeted page in a new incognito window, "1" is successfully logged to the console.



      But if I refresh the page, nothing is logged to the console. I can navigate to and from the page, but nothing is ever logged.



      I can only get it to work again if I close my incognito session and start a new one.



      It's as if Optimize is only executing the script the first time the variation is seen by a session user?



      How can I get the JS to run every time the page is viewed in a session?



      Note: I have tried placing the code in the body and the head, both after opening tag and after closing tag.







      javascript google-optimize






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 2 '18 at 10:19









      FijjitFijjit

      5821726




      5821726
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Thanks to some help from a Google Employee here, I managed to work out that JQuery was the hold-up. On first incognito load, Optimize wasn't yet cached and was taking longer to load than JQuery, so by the time it executed my custom JS could run OK.



          But on refresh, the cached Optimize was loading before JQuery, meaning that my custom JS was failing.



          The solution was to strip out JQuery and do what I needed with plain vanilla Javascript.



          For my simple example above, I can simply call console.log(1); without wrapping it.



          In my real work, I was trying to set placeholder text in an input field. Doing this in plain Javascript worked fine:



          document.getElementById('theElement').placeholder = 'The Text';



          Hope this helps someone.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53116649%2fwhy-doesnt-google-optimize-javascript-execute-on-page-refresh%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Thanks to some help from a Google Employee here, I managed to work out that JQuery was the hold-up. On first incognito load, Optimize wasn't yet cached and was taking longer to load than JQuery, so by the time it executed my custom JS could run OK.



            But on refresh, the cached Optimize was loading before JQuery, meaning that my custom JS was failing.



            The solution was to strip out JQuery and do what I needed with plain vanilla Javascript.



            For my simple example above, I can simply call console.log(1); without wrapping it.



            In my real work, I was trying to set placeholder text in an input field. Doing this in plain Javascript worked fine:



            document.getElementById('theElement').placeholder = 'The Text';



            Hope this helps someone.






            share|improve this answer




























              0














              Thanks to some help from a Google Employee here, I managed to work out that JQuery was the hold-up. On first incognito load, Optimize wasn't yet cached and was taking longer to load than JQuery, so by the time it executed my custom JS could run OK.



              But on refresh, the cached Optimize was loading before JQuery, meaning that my custom JS was failing.



              The solution was to strip out JQuery and do what I needed with plain vanilla Javascript.



              For my simple example above, I can simply call console.log(1); without wrapping it.



              In my real work, I was trying to set placeholder text in an input field. Doing this in plain Javascript worked fine:



              document.getElementById('theElement').placeholder = 'The Text';



              Hope this helps someone.






              share|improve this answer


























                0












                0








                0







                Thanks to some help from a Google Employee here, I managed to work out that JQuery was the hold-up. On first incognito load, Optimize wasn't yet cached and was taking longer to load than JQuery, so by the time it executed my custom JS could run OK.



                But on refresh, the cached Optimize was loading before JQuery, meaning that my custom JS was failing.



                The solution was to strip out JQuery and do what I needed with plain vanilla Javascript.



                For my simple example above, I can simply call console.log(1); without wrapping it.



                In my real work, I was trying to set placeholder text in an input field. Doing this in plain Javascript worked fine:



                document.getElementById('theElement').placeholder = 'The Text';



                Hope this helps someone.






                share|improve this answer













                Thanks to some help from a Google Employee here, I managed to work out that JQuery was the hold-up. On first incognito load, Optimize wasn't yet cached and was taking longer to load than JQuery, so by the time it executed my custom JS could run OK.



                But on refresh, the cached Optimize was loading before JQuery, meaning that my custom JS was failing.



                The solution was to strip out JQuery and do what I needed with plain vanilla Javascript.



                For my simple example above, I can simply call console.log(1); without wrapping it.



                In my real work, I was trying to set placeholder text in an input field. Doing this in plain Javascript worked fine:



                document.getElementById('theElement').placeholder = 'The Text';



                Hope this helps someone.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 16:47









                FijjitFijjit

                5821726




                5821726
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53116649%2fwhy-doesnt-google-optimize-javascript-execute-on-page-refresh%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine