Tagged: google analytics
-
AuthorPosts
-
October 22, 2013 at 1:06 am #178774
reference for where this is coming from.
http://www.blastam.com/blog/index.php/2013/03/how-to-track-downloads-in-google-analytics-v2/
Hey guys this may be above tech support, but maybe not for you gurus. What I want to do is track outbound links, and download actions and such with my google analytics account. I found this script above that automates the process but I am not quite sure how to add a js script to the enfold theme. What I did was simply add the new script below my google analytics script in the child them backend. when I inspect a page I see the script referenced in the head after my normal google analytics script and if I am reading right you guys are referencing a newer jquery library than this needs. you have 1.10.2 or something. Can you look at my site and see if you know why this might not be working. my site is http://www.jackandaddi.comthe documentation is straight forward. just be sure to include the jQuery library reference in your section before the call to the tracking code. example <script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js” type=”text/javascript”></script>
For those who have a lot of links and would like to dynamically detect clicks on links to file downloads, we have provided updated code below. As before, this code requires the jQuery JavaScript library to be set before the code. It can be placed in its own .js file and should be placed in the <head> of your pages.
<script type="text/javascript"> if (typeof jQuery != 'undefined') { jQuery(document).ready(function($) { var filetypes = /\.(zip|exe|dmg|pdf|doc.*|xls.*|ppt.*|mp3|txt|rar|wma|mov|avi|wmv|flv|wav)$/i; var baseHref = ''; if (jQuery('base').attr('href') != undefined) baseHref = jQuery('base').attr('href'); jQuery('a').on('click', function(event) { var el = jQuery(this); var track = true; var href = (typeof(el.attr('href')) != 'undefined' ) ? el.attr('href') :""; var isThisDomain = href.match(document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0]); if (!href.match(/^javascript:/i)) { var elEv = []; elEv.value=0, elEv.non_i=false; if (href.match(/^mailto\:/i)) { elEv.category = "email"; elEv.action = "click"; elEv.label = href.replace(/^mailto\:/i, ''); elEv.loc = href; } else if (href.match(filetypes)) { var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined; elEv.category = "download"; elEv.action = "click-" + extension[0]; elEv.label = href.replace(/ /g,"-"); elEv.loc = baseHref + href; } else if (href.match(/^https?\:/i) && !isThisDomain) { elEv.category = "external"; elEv.action = "click"; elEv.label = href.replace(/^https?\:\/\//i, ''); elEv.non_i = true; elEv.loc = href; } else if (href.match(/^tel\:/i)) { elEv.category = "telephone"; elEv.action = "click"; elEv.label = href.replace(/^tel\:/i, ''); elEv.loc = href; } else track = false; if (track) { _gaq.push(['_trackEvent', elEv.category.toLowerCase(), elEv.action.toLowerCase(), elEv.label.toLowerCase(), elEv.value, elEv.non_i]); if ( el.attr('target') == undefined || el.attr('target').toLowerCase() != '_blank') { setTimeout(function() { location.href = elEv.loc; }, 400); return false; } } } }); }); } </script>
October 22, 2013 at 7:08 am #178901Hi allegrabillings!
I’m not familiar with the code but you can create a new file inside the js folder called “googlelink.js” then add the code above but strip off or remove the “<script type=”text/javascript”></script>” part. Edit functions.php, find this code:
wp_register_script( 'wp-mediaelement', $template_url.'/js/mediaelement/mediaelement-and-player.min.js', 'jquery', "1", true);
Below, add this:
wp_register_script( 'googlelink', $template_url.'/js/googlelink.js', 'jquery', "1", true);
Again, find this code:
wp_enqueue_script( 'wp-mediaelement' );
Below, add this:
wp_enqueue_script( 'googlelink' );
The script will be included on the header.
Regards,
IsmaelOctober 22, 2013 at 9:56 pm #179242Thanks Ismael I will try to load it that way. Quick question though. How to you modify this for a child theme change.
the /* Register frontend javascripts:*/ function is pretty large. What do you put and where in a child theme directory?Do I place this in my child functions.php and then place the new .js file in a js folder in the child directory?
add_filter('avia_load_js', 'avia_include_js_template', 15, 1); function avia_include_js_template($paths) { $template_url = get_stylesheet_directory(); array_unshift($paths, $template_url.'/js/'); return $paths; }
I am assuming that is wrong though. as the
wp_register_script( ‘googlelink’, $template_url.’/js/googlelink.js’, ‘jquery’, “1”, true);
and
wp_enqueue_script( ‘googlelink’ );
were never loaded.Will you please map out the change for a child setup please? thanks.
October 23, 2013 at 1:01 pm #179634Hi!
Use following code to load the javascript file from the child theme
if(!is_admin()) add_action('wp_enqueue_scripts', 'avia_register_child_frontend_scripts', 100, 1); function avia_register_child_frontend_scripts() { $child_theme_url = get_stylesheet_directory_uri(); //register js wp_register_script( 'avia-js-child-theme', $child_theme_url.'/js/googlelink.js', array('jquery'), 1, false ); wp_enqueue_script( 'avia-js-child' ); }
and place the googlelink.js file into a js folder inside your child theme folder.
Best regards,
PeterOctober 23, 2013 at 4:45 pm #179770Thanks Peter I will give this a try and see if the tracking script then loads. I really want to be able to track pdf downloads and outbound links on the site.
October 23, 2013 at 4:57 pm #179784Hey!
Great, let us know if the script doesn’t load.
Cheers!
PeterOctober 23, 2013 at 9:50 pm #179960Found one mistake Peter. Just a typo. It wasn’t loading and after analyzing the parent functions file and comparing with the code you wrote I figured it was just a typo. so after changing it, the script loads. Now I will have to wait a day and see if the links are being tracked.
wp_register_script( 'avia-js-child-theme', $child_theme_url.'/js/googlelink.js', array('jquery'), 1, false ); wp_enqueue_script( 'avia-js-child' );
needs to be
wp_enqueue_script( 'avia-js-child-theme' );
but after making that change it loads. Thank you
-
AuthorPosts
- The topic ‘script for google analytics link tracking’ is closed to new replies.