• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Using enqueue script for Google Analytics autotrack.js

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Using enqueue script for Google Analytics autotrack.js

This topic is: not resolved

Tagged: autotrack.js, enqueue script, google analytics

  • This topic has 5 replies, 3 voices, and was last updated 9 years, 1 month ago by joycegrace.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • March 31, 2016 at 7:55 pm #182614
    joycegrace
    Participant

    Hi there,
    Sorry for the folks who see this as an easy and google-able subject. I'm not much of a php coder, but I'm trying to get Google Analytics' new autotrack.js enqueued in a theme's functions.php file using Genesis specifically.

    I was hoping someone would be willing to pay it forward and help me by providing a copy-paste solution I could put in functions.php for this?

    The link would be here: https://cdnjs.com/libraries/autotrack (so using 'https://cdnjs.cloudflare.com/ajax/libs/autotrack/0.6.4/autotrack.js' )

    I did try to search this and I'm just not sure if this will work:

    add_action( 'genesis_before', 'add_ga_autotrack' );
    function add_ga_autotrack() {
        if ( current_filter() == 'genesis_before' )
            echo '<script async src='https://cdnjs.cloudflare.com/ajax/libs/autotrack/0.6.4/autotrack.js'></script>';
     
    }

    But I can't figure out where the "enqueue" part goes.

    This is my attempt after reading this:
    http://wordpress.stackexchange.com/questions/59326/how-to-add-code-just-below-opening-body-tag-in-genesis-framework

    Did I do it wrong?

    To pre-emptively answer some questions you may have:

    If you're wondering why I don't just use Genesis Theme settings to add this in the Header scripts area, it's because this needs to go on a multisite network, and applied to multiple sites using the same theme. It would be impractical to manage this across say, 70 different sites. We need it to load automatically for all the sites using a particular theme.

    If you're wondering why I don't just 'test it out' and see if it works, well, it's a bit tricky. The data doesn't always get sent to Google Analytics right away. Yes, there is "real time" tracking. But the problem is - if the data doesn't show up, it will be very difficult to tell whether it's because of an error setting up the Google Analytics part of this, or if it's because the php is wrong, and is not loading the script at all.

    So I was hoping someone more experienced in PHP could help me out here with a more sure-fire way to get this script loaded on the sites.

    Thank you super much whoever can help.


    Find me at Joyce Grace (http://www.joycegrace.ca)

    March 31, 2016 at 10:22 pm #182617
    Victor Font
    Moderator

    You really don't need to use wp_enqueue_script for the Google scripts. You could just cut and paste them in the header script area on the Genesis/Theme Settings page. No need to do anything in php for these scripts. Don't forget to change the UA-XXXXX-Y to your sites UA tracking number.

    <script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', 'UA-XXXXX-Y', 'auto');
    ga('require', 'autotrack');
    ga('send', 'pageview');
    </script>
    <script async src='https://www.google-analytics.com/analytics.js'></script>
    <script async src='path/to/autotrack.js'></script>

    Regards,

    Victor
    https://victorfont.com/
    Call us toll free: 844-VIC-FONT (842-3668)
    Have you requested your free website audit yet?

    March 31, 2016 at 11:56 pm #182623
    joycegrace
    Participant

    Hi Thanks for replying, but I explained in my original post why the theme settings won't work for this particular case. I really need this particular script to go in functions.php

    It doesn't have to load before or after the analytics main script, it just needs to load on the site, on every page.


    Find me at Joyce Grace (http://www.joycegrace.ca)

    April 1, 2016 at 11:55 am #182679
    carasmo
    Participant

    This works. It is the last script on your page in the body on all pages. Your original would have thrown a parse error since you used single ticks on the src and single on the echo:

    /** ====================================================================================
     * Add autotrack script in the footer of the page on all pages
    ==================================================================================== **/
    
    function add_ga_autotrack() {
    
            echo '<script async src="https://cdnjs.cloudflare.com/ajax/libs/autotrack/0.6.4/autotrack.js"></script>';
     
    }
    add_action( 'genesis_after', 'add_ga_autotrack', 99 );
    

    Genesis Theme Customization and Help

    April 5, 2016 at 7:15 pm #183015
    joycegrace
    Participant

    You are so nice! Thank you! I will test this out and see if it tracks ok and report back


    Find me at Joyce Grace (http://www.joycegrace.ca)

    April 7, 2016 at 2:10 am #183102
    joycegrace
    Participant

    It seems to add the script, but shouldn't this use wp_enqueue? Just curious. Sorry if it's a dumb question!

    Would this work?

    function load_script()
    {
        if (!is_admin()) {
            wp_register_script('autotrack', '//cdnjs.cloudflare.com/ajax/libs/autotrack/0.6.4/autotrack.js', array(), '0.6.4');
            wp_enqueue_script('autotrack');
        }
    }
    add_action('wp_footer', 'load_script');

    Find me at Joyce Grace (http://www.joycegrace.ca)

  • Author
    Posts
Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2025 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble