Community Forums › Forums › Archived Forums › General Discussion › Using enqueue script for Google Analytics autotrack.js
Tagged: autotrack.js, enqueue script, google analytics
- This topic has 5 replies, 3 voices, and was last updated 8 years, 5 months ago by joycegrace.
-
AuthorPosts
-
March 31, 2016 at 7:55 pm #182614joycegraceParticipant
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-frameworkDid 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 #182617Victor FontModeratorYou 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 #182623joycegraceParticipantHi 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 #182679carasmoParticipantThis 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 );
April 5, 2016 at 7:15 pm #183015joycegraceParticipantYou 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 #183102joycegraceParticipantIt 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)
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.