Reputation: 49
I am trying to integrate Google Universal Analytics into my website. I have started to see basic reporting (page views, uniques, etc.) under Behavior > Overview
But still see nothing for real-time reporting under Realtime > Overview
And I cannot seem to get any events to fire.
I have this code snippet at the very end of :
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-41505208-2', 'auto');
ga('send', 'pageview');
//ga(‘set’, ‘&uid’, {{USER_ID}}); // Set the user ID using signed-in user_id.
$(".sub-menu li a").click(function(){
//window.analytics.trackEvent('nav', 'tap', 'filter');
//ga('nav', 'tap', 'filter');
//ga('send', 'event', { eventCategory: 'nav', eventAction: 'tap', eventLabel: 'filter'});
ga('send', 'event', 'nav', 'tap', 'filter');
});
</script>
I have tried to move my sample event to other places with no luck ... also not sure which of the event lines is the one you're supposed to use with Universal, so have tried each one with no luck.
So frustrating. The old Google Analytics always worked so easily.
I have this site located at a directory under another site, does that matter?
Upvotes: 0
Views: 980
Reputation: 852
Your Custom Events are not firing because at the time of the evaluation, $(".sub-menu li a")
does not match any element in the DOM, as it has not finished loading.
You should wrap your call in a $(document).ready()
callback:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-41505208-2', 'auto');
ga('send', 'pageview');
//ga(‘set’, ‘&uid’, {{USER_ID}}); // Set the user ID using signed-in user_id.
// From your original post:
$(document).ready(function () {
$('.sub-menu li a').click(function () {
//window.analytics.trackEvent('nav', 'tap', 'filter');
//ga('nav', 'tap', 'filter');
//ga('send', 'event', { eventCategory: 'nav', eventAction: 'tap', eventLabel: 'filter'});
ga('send', 'event', 'nav', 'tap', 'filter');
});
});
</script>
Alternatively, you could also use the .on()
method, for live evaluation:
$(document).on('click', '.sub-menu li a', function () {
ga('send', 'event', 'nav', 'tap', 'filter');
});
Upvotes: 2