[Support request] How do I add custom JavaScript immediately after the opening tag in post

Home Forums Support [Support request] How do I add custom JavaScript immediately after the opening tag in post

Home Forums Support How do I add custom JavaScript immediately after the opening tag in post

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1434958
    John

    Hello,
    I need to do so Geo Redirects using Geotargetly.com.
    To do this I need to “Copy and paste the code (generated by Geotargetly) immediately after the opening <head> tag in your website’s HTML”

    https://help.geotargetly.com/en/articles/1729256-javascript-integration

    QUESTIONS:
    1. Currently this is for only 1 post on the entire website
    2. Can you explain where and how I can insert this code?
    3. If I do as per my gut feeling below, is this for the entire website or just the 1 post I am doing redirection for? (I don’t understand CSS/HTML)

    I can see the: GP Appearance – Theme Editor – Header PHP.
    BUT I don’t knowhow to do it

    The JS code is
    <script>
    (function(g,e,o,t,a,r,ge,tl,y,s){
    g.getElementsByTagName(o)[0].insertAdjacentHTML(‘afterbegin’,'<style id=”georedirect1599558256920style”>body{opacity:0.0 !important;}</style>’);
    s=function(){g.getElementById(‘georedirect1599558256920style’).innerHTML=’body{opacity:1.0 !important;}’;};ge=0;y=[];r=[];tl=function(){if(ge<1){
    a=Math.round(new Date().getTime()/(86400000*10))-1837+ge;r[ge]=’g’+198084335*a+’.co’;t=g.getElementsByTagName(o)[0];y[ge]=g.createElement(e);y[ge].async=true;
    y[ge].src=’//’+r[ge]+’/gr?id=-MGh5VVBxIyPAI0OnzR2&refurl=’+g.referrer+’&winurl=’+encodeURIComponent(window.location);t.parentNode.insertBefore(y[ge],t);
    y[ge].onerror=function(){y[ge].remove();ge++;tl();};}};tl();georedirect1599558256920loaded=function(redirect){var to=0;if(redirect){to=5000};
    setTimeout(function(){s();},to)};setTimeout(function(){s();}, 3000);
    })(document,’script’,’head’);
    </script>

    So is it a case of adding this between the <head> and closing </head> (in GP Appearance – Theme Editor – Header PHP)
    <head><script>
    (function(g,e,o,t,a,r,ge,tl,y,s){
    g.getElementsByTagName(o)[0].insertAdjacentHTML(‘afterbegin’,'<style id=”georedirect1599558256920style”>body{opacity:0.0 !important;}</style>’);
    s=function(){g.getElementById(‘georedirect1599558256920style’).innerHTML=’body{opacity:1.0 !important;}’;};ge=0;y=[];r=[];tl=function(){if(ge<1){
    a=Math.round(new Date().getTime()/(86400000*10))-1837+ge;r[ge]=’g’+198084335*a+’.co’;t=g.getElementsByTagName(o)[0];y[ge]=g.createElement(e);y[ge].async=true;
    y[ge].src=’//’+r[ge]+’/gr?id=-MGh5VVBxIyPAI0OnzR2&refurl=’+g.referrer+’&winurl=’+encodeURIComponent(window.location);t.parentNode.insertBefore(y[ge],t);
    y[ge].onerror=function(){y[ge].remove();ge++;tl();};}};tl();georedirect1599558256920loaded=function(redirect){var to=0;if(redirect){to=5000};
    setTimeout(function(){s();},to)};setTimeout(function(){s();}, 3000);
    })(document,’script’,’head’);
    </script></head>

    CAN YOU PLEASE SCREENSHOT EXACTLY WHERE TO PUT IT OR DO IT FOR ME?

    Thank you

    #1435156
    David
    Staff
    Customer Support

    Hi there,

    you can use the Hook Element to add scripts:

    https://docs.generatepress.com/article/hooks-element-overview/

    1. Enable the Elements module in Appearance > GeneatePress/
    2. Go to Appearance > Elements –> Add New -> Hook.
    3. Add the script to the Text field.
    4. Select the wp_head hook from the Hook list – this will place it inside the <head></head> tags.
    5.TGo to Display Rules tab and set the Location eg. Post > My Post to where you want the script to run.

    For reference in case you need to add other scripts using Hooks – these are the the main WP hooks you would need:

    wp_head – the <head></head> tags
    wp_body_open – immediately after the opening <body> tag
    wp_footer – inside the footer.

    #1435175
    John

    OK, Great!
    Just before I do that.
    I also noticed there is a plugin

    https://dashboard.geotargetly.com/geo-redirect
    The dashboard says to me (logged in):
    Install our WordPress Geo Redirect Plugin and enter the ID below
    See full instructions for WordPress (https://help.geotargetly.com/en/articles/1997680-wordpress-integration)

    -MGh5VVBxIyPAI0OnzR2

    I then go and see the Plugin
    https://wordpress.org/plugins/geo-targetly-geo-redirect/

    This seems easier for me being non-techy

    QUESTIONS
    1. I need to do this for another post AND the entire website for one location.
    So there will be 3 such Geotargetly JS codes to include.
    2. The plugin seems easier, what will work faster for my website the 3 long JS codes OR 1 plugin (with 3 shorter codes inside the plugin (e.g. -MGh5VVBxIyPAI0OnzR2 is the first one)
    3. How could I test that plugin for speed?

    I have read the wordpress.org page and cannot see anything about the speed, there is no reviews and only 2 support questions.

    Thank you

    #1435256
    David
    Staff
    Customer Support

    For non techie i would use their plugin. I cannot see it affecting page speed as it will be simply inserting the scripts for you. It also means if there are any issues with the Geo Targetly you can explain you’re using their plugin

    #1435260
    John

    Thanks David,
    I actually was able to do it using the Hook and I liked that it could only be added to the one post of concern.
    I did it as a test 30 mins ago and I noticed a 2-3 second lag. Meaning when I entered
    https://earnfromyourlaptop.com/usana-health-sciences-review/
    I saw it load (slowly) then the USANA post flashed up for 1-2 seconds before being redirected to the 404 post that I made in elementor (typing hands https://earnfromyourlaptop.com/404-page/).
    How was the speed for you?

    QUESTION
    What will load faster for the post
    1. The hook for that specific post
    OR
    2. The plugin?

    My friend told me:
    An Async script loads after DOM (Document ready)
    If you really want to prevent it it must not load Async
    It must load in the head before anything else
    Which is the opposite of Async
    And will slow your page

    What are you thoughts on the fastest way to do this?
    (I realise that I haven’t blocked your location, only New South Wales but none of Europe if that is where you are)

    #1435280
    David
    Staff
    Customer Support

    Hmmm… can you try disabling your cache plugin as the issue we may have is the Cached page is loading before the script completes the redirect. Once disabled let me know so i can take a look ( Yes, i am in Europe – rest of the team are in Canada and Asia ).

    #1435971
    John

    OK David,
    I will wait for you. Please tell me the city and region/state where you are.
    Then I will
    1. add that location to the list of geoblocking for the redirect
    2. disable WP Rocket
    Get you to take a look.

    I am in Perth/Singapore GMT +8, So I assume I am 7 or 8 hours ahead of you so will wait for your response and then ask you to test it and see if you see the original post for 2-3 seconds like my friend in Perth sees (even after I have redirect in place for Perth)
    Thanks again

    #1436231
    David
    Staff
    Customer Support

    I am based in the United Kingdom

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.