Onclick Event not recording in Google Analytics - google-analytics

My google analytics events recently stopped working for an unknown reason. I didn't make any changes to my code and I'm pretty sure nobody else did as well. I checked through it and it looks like it should be working. Can anyone spot an issue with this?
First my header script:
<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-?????????-1', 'auto');
ga('send', 'pageview');
</script>
Next the button which triggers the event:
<input name="button" type="button" class="button" onClick="return checkValues(true); ga('send', 'event', { eventCategory: 'lead', eventAction: 'click', eventLabel: 'clickonform'}); " value="Submit" width="20px" />
And of course my Analytics settings themselves.
(3) Goal details
Category - Equals to - lead
Action - Equals to - click
Label - Equals to - clickonform
Value - Greater than - 0
Use the Event value as the Goal Value for the conversion
Maybe this is a friday thing and I am overlooking something obvious, but my mind just can't wrap around this, Any help would be greatly appreciated!

You are returning the value of "checkvalue(true)" before the event is sent. Try to send the event first, and then return.
Also, you are not passing in an event value, but your goal is configured to convert if the value is greater than 0. Might need to check on that.

Related

GTAG for Optimize ID

What is the gtag.js equivalent of the following analytics.js Google Optimize integration:
ga('set', 'expId', '$experimentId'); // The id of the experiment the user has been exposed to.
ga('set', 'expVar', '$chosenVariation'); // The index of the variation shown to the user.
I could not find anything documented in Google site.
Some places I looked:
https://developers.google.com/analytics/devguides/collection/gtagjs/migration
https://developers.google.com/analytics/devguides/collection/analyticsjs/experiments
Update - 08/23/2018 - using the response from #AnkDasCo
Further, my Optimize experiment is configured as SERVER_SIDE which means I do not want Optimize to assign the experiment and variant to the site. Instead, that's something I would like to control from my end. I have injected the following in the page:
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('set', {'expId': 'xxxxxxxxxxxxxxxxxxxxxx'});
gtag('set', {'expVar':'1'}); // set to variant id 1
gtag('config', 'UA-xxxxxxxx-y', { 'optimize_id': 'GTM-xxxxxxx'});
However, the variant assignment is still being controlled by Optimize and not by the script above. In the above example I am asking it to be set to variant id 1, but it's not happening.
Even setting the experiment/variant in a single statement as this does not work either:
gtag('set', {'exp': 'xxxxxxxxxxxxxxxxxxxxxx.1'});
Any thought what I might be missing. Any help will be greatly appreciated.
You can still set values that persist across gtag calls on the page.
gtag('set', {'expId': 'xxxxxxxxxxxxxxxxxxxxxx'});
gtag('set', {'expVar':'1'});
This analytics.js way works:
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxxxxxx-y', 'auto');
ga('set', 'exp', 'xxxxxxxxxxxxxxxxx.1');
ga('require', 'GTM-xxxxxx');
ga('send', 'pageview');
</script>
Note that since the experiment is set as SERVER_SIDE, the effect of setting the experiment and the variant needs to be handled by the server and Optimize plays no role in that. If you do server side, you do it all yourself - you can create an experiment for "page A" and then send experiment hits from "page B" and things will still work fine.

Google Analytics Event tracking for Goals

I am having an issue in using the Google Goals for my My Event Tracking. I have added Google even tracking code to my web page. However, I cannot check for any conversions in my Google Goals but the event result can be shown in the Google Analytic --> Behavior --> Events Overview.
My Code is below
` <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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-87810245-1', 'auto');
ga('send', 'pageview');
</script>
<script>
$('#contact-form').on('submit', function(event) {
// Prevent the browser's default form submission action.
event.preventDefault();
ga('send', 'event', {
eventCategory: 'Contact',
eventAction: 'Information Request',
eventLabel: 'Contact Form',
hitCallback: function() {
$('contact-form').trigger('submit');
}
});
});
</script>`
Code for my Submit Button:
<input type="submit" id="submit" value="Validate and Submit" onclick="ga('send', 'event', 'form', 'submit', 'order sent', 10);"/>
My Google Goals:
enter image description here
Google Analytics Events Overview:
So you've got two events going on, one for the click of the submit button (to track intent perhaps) and one for the form submission itself. Your goal is configured to expect a value Greater than 10, but your form submission event is not passing a value in. You should either add a value (greater than 10), or remove the value from the goal configuration. Also your screenshot shows likely just the click event (since there's a value of 10 associated with it), but you'd probably be more interested in the actual submissions themselves.

Double google anaylatics on my template?

I'm not a coder though I've learned alot from this board. My friend set up a template for me to work from. there is 2 versions of google anaylatic script in there and i'm not sure which to use?
<script>
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
ga('create','UA-XXXXX-X','auto');ga('send','pageview');
</script>
or....
<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-XXXXX-X', 'auto'); ga('send', 'pageview'); </script>
Thanks so much!
As per the comments, the answer is: leave one with the correct GA Id. Remove any of them in the head tag, and leave the final snippet before closing the body tag.

Google Analytics shows too many pageviews

I have a strange issue with google Analytics on a website. This site normally has about 1000 - 1500 pageviews / day (and approximately 500-600 unique users).
Since a week analytics measures between 5000 and 15000 pageviews / day, but the unique users remains unchanged. I have never had so many pageviews / day in the last 2 years, so this values can't be correct.
I checked my Chrome network panel and noticed that http://www.google-analytics.com/collect?xxxx is triggered to often. Nothing has changed on the site, so what could cause such a behaviour?
An other problem is my bounce rate. Before I updated to Universal Analytics I used the following code to trigger an event gaq.push(['_trackEvent', 'Category', 'Action', undefined, 50, true]); ("true" to signalize a non-interaction-event).
After the update I used
ga('send', 'event', 'xxx', 'xxx', 'xxx');
The bounce rate decreased to 0% (the events has been counted as "interaction-event").
I found the notice to use the following ga('set', 'nonInteraction', true); to set the non-interaction flag. But where should I use this?
Should I start with the analytics code to track the pageview
(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-xxxxxx-xx', 'example.com');
ga('require', 'displayfeatures');
ga('send', 'pageview');
then set the non-interaction-flag
ga('set', 'nonInteraction', true);
and at the end trigger an event in my page content
e.g. onload="ga('send', 'event', 'xxx', 'xxx', 'xxx');"
?
see eventTrackerGuide.
non-interaction (optional)
A boolean that when set to true, indicates that the event hit will not be used in bounce-rate calculation
Every hit after the code ga('set', 'nonInteraction', true); will be a non-interaction hit.
If you want make a specific hit to non-interaction, just add non-interaction option on the hit. For example: ga('send', 'pageview', {'nonInteraction': true});
Try:
ga('send', 'event', 'xxx', 'xxx', 'xxx', undefined, true);
undefined - if you don't have a value of event

Google Analytics Event Tracking onClick Code

I'm trying to set up event tracking on my web site but can't get it working correctly.
My tracking code:
<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-420xxxxxxx', 'mywebsite.org');
ga('send', 'pageview');
</script>
My event tracking code:
Purchase Details
You are mixing classic code with universal code. It will not work. You need to replace this:
_gaq.push(['_trackEvent', 'Button', 'Click', 'Purchase Details',, false]);
With this:
ga('send', 'event', 'Button', 'Click', 'Purchase Details');
GAJS reference for Events: https://developers.google.com/analytics/devguides/collection/analyticsjs/events#implementation
Event hits can be sent using the send command. According to new analytics.js
Syntax:
ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
For example if you want to track purchase event
ga('send', 'event', 'Button', 'Click', 'Purchase Details');
Here:-
eventCategory is Button. It is required field and its Value Type is text
eventAction is Click. It is required field and its Value Type is text
eventLabel is Purchase Details. It is optional field and its Value Type is text
eventValue is null. It is optional field and its Value Type is integer
it looks like you're using the new analytics.js instead of ga.js, so you'll want to use the proper event tracking method set:
ga('send', 'event', 'category', 'action');

Resources