• Resolved sowmya

    (@sowmya002gmailcom)


    Hello,
    I have a wordpress site having around 300 pages. We have developed our own childtheme based on genesis.
    I would like to add a tracking code to 2 of my pages. 1 is homepage and one more normal WP page.
    The code is a javascript which has 2 parts. First part of it to should be pasted immediately after the <body> tag and the second part immediately before the </body> tag.
    Please let me know how can I achieve it in my homepage and other pages.
    (NB: The code should not be pasted in all the pages. Only specific pages should have it)
    Thanks!
    Sowmya
    Concultant Web Developer

Viewing 9 replies - 1 through 9 (of 9 total)
  • You could create custom templates for each page that you want to individually add javascript, alternatively, you could add some logic checks to your header.php to check for the specific pages in the places you need to add the js.

    Thread Starter sowmya

    (@sowmya002gmailcom)

    Thanks Andrew. What about the home page? Can we follow the same method that you have suggested? I am not sure about the home page. Please clarify.
    Thanks!

    You need to make a page.php template that will call a header and footer that has the tracking code you want, and a header.php and footer.php template that contains the tracker. See https://codex.www.ads-software.com/Pages#Creating_Your_Own_Page_Templates

    Your header can be called header-1.php and be called in the page.php template by <?php get_header('1'); ?> Same thing for the footer: <?php get_footer('1'); ?> or <?php get_footer('tracking'); ?> and footer-tracking.php

    Thread Starter sowmya

    (@sowmya002gmailcom)

    Hello Songdogtech,
    Thanks for the reply!
    I am clear about what you have mentioned above. I will try it out. But what about the home page? The home page I am using is not a static one. So will the above method me applicable? If not what can I do for adding the code to the homepage? Please clarify.
    Thanks!

    What do you mean by “not static?”

    Thread Starter sowmya

    (@sowmya002gmailcom)

    Hello Songdogtech,
    Under Settings–>Reading, out of the 2 options available for the front page displays, we have chosen the latest posts.
    If we select the option ‘a static page’ we can choose one of our pages as our home page. If so we could have used the same method that you have told for adding the tracking code.
    But since we have selected the recent posts option I am not clear how to use your method for the home page.
    Thanks!
    Sowmya

    Ah, then recent posts option will probably use index.php. If you don’t have an archive.php and a category.php template, index.php might be used for them, too. So try a conditional in index.php, if that template is used for recent posts.

    <?php if (is_front_page() { ?>
    
    tracking code
    
    <?php } ?>
    Thread Starter sowmya

    (@sowmya002gmailcom)

    Hello Songdogtech,
    Thanks for your reply. I placed the code in the above mentioned way but not in index.php. I placed it in the childtheme’s functions.php.

    /** Add top part of trackingcode */
    <?php
    add_action (‘genesis_before’,’trackingcode_do_top’);
    function trackingcode_do_top()
    {
    if ( is_home())
    {
    ?>
    tracking code
    <?php
    }
    }

    /** Add bottom part of trackingcode */
    add_action (‘genesis_after’,’trackingcode_do_bottom’);
    function trackingcode_do_bottom()
    {
    if ( is_home())
    {
    ?>
    tracking code
    <?php
    }
    }
    The above code works fine.

    Thanks!
    Sowmya

    Thread Starter sowmya

    (@sowmya002gmailcom)

    Thanks for all your support!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Add tracking code to individual page’ is closed to new replies.