Custom Events not received in Google Anallytics Dashboard - google-analytics

I'm trying to send a custom event to my GA. However, GA does not show any data in the dashboard.
Here's my UPDATED code:-- (As suggested by #Дмитро Булах)
<html>
<head>
<title></title>
<meta charset="utf-8"/>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.googletagmanager.com/gtag/js?id=UA-XXX-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXX-1');
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#button-cta').click(function() {
alert(111)
gtag('event', 'click', {
'event_category': 'Deal click',
'event_label': 'URL1',
'value': '1'
});
alert(111)
});
});
</script>
<body>
<button id="button-cta">1111</button>
</body>
</html>
I view my data in Reports -> RealTime -> Events. It shows nothing.
PS: I'm developing this locally, and have opened a HTML file in the browser.

since you're using gtag.js snippet you need to use gtag(...) object to track events, like
gtag('event', <action>, {
'event_category': <category>,
'event_label': <label>,
'value': <value>
});
ga(...) works with older analytics snippet, analytics.js
More on event tracing with gtag(...) here

The GTAG event method is the right way to go as per the previous answer by #Дмитро Булах . If you are testing locally on a HTML I don't think the event will fire to Google Analytics, I would suggest implementing and testing on a staging environment if possible and then testing the event is firing either using the Event Tracking tracker plugin or Real Time report in Google Analytics.

Related

Sending an event to Google Analytics doesn't work

In the header of every page I have the following integration code from Google Analytics:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-1');
</script>
XXXXXXXXX is obviously replaced with our ID.
In the footer of a page where contact form exists, I have placed the following code:
document.addEventListener( 'wpcf7mailsent', function( event ) {
ga( 'send', {
hitType: 'event',
eventCategory: 'contact-form',
eventAction: 'contact-form-submission-mailsent',
eventLabel: 'Contact CTA'
});
});
The listener works well, and has been tested with console.log after a successful submit. However, no event is visible in Google Analytics under "Behavior" -> "Events". There are no other customizastion in Google Analytics, it is pretty basic.
Any help or guidance is much appreciated.
You can't mix Universal Analytics code and gtag code. If you have in head gtag snippet, you have to use its syntax for send an event:
gtag('event', <action>, {
'event_category': <category>,
'event_label': <label>,
'value': <value>
});
https://developers.google.com/analytics/devguides/collection/gtagjs/events

Google Analytics Events are Not Registering

I am in need of assistance. I am trying to implement Custom Event Tracking for a form submission as both an Analytics Goal, and a Conversion Goal for Google Optimize.
Here is a screenshot of the Google Analytics Goal:
I have my Google Analytics in the head (with sensitive information redacted with XXXXX) as such:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=XXXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'XXXXXXXXXXX', { 'optimize_id': 'XXXXXXXXXXX'});
</script>
And then I have tried to fire the custom event with both:
ga('send', {
hitType: 'event',
eventCategory: 'mailchimp',
eventAction: 'subscribe',
eventLabel: 'soft-lead-form'
});
and also:
gtag('event', 'play', {
'event_category': 'mailchimp',
'event_label': 'soft-lead-form'
});
I have confirmed that analytics is instanced effectively by writing to the console with:
if (gtag) {
console.log("ga present");
}
And I have confirmed that the user action triggers the ga call by writing to the console in the same function with:
$("#mc-embedded-subscribe-form").submit(function(){
gtag('event', 'play', {
'event_category': 'mailchimp',
'event_label': 'soft-lead-form'
});
console.log("ga Event Submitted");
});
I would GREATLY APPRECIATE any help. I've been through pages of documentation and, despite trying/testing multiple points in this process, I can not get the event to appear in Either Analytics or Optimize. This is a crucial piece for our marketing efforts and I very much want to make it work.
Thank you so much!
Gary
Surely the event with ga(...) you have to remove it because you are using gtag.
If you are seeing the events real time, then you need to wait for at least 24-72 hrs to get the data updated under Behaviour -> Events -> Overview.

Analytics pageview/ event code is result in stats

I am trying to get statistics about sign ups on my website.
The analytics code is in the header:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=...."></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', '....');
</script>
Then I have a form and sending it (with a page reload, not ajax) and in the success snippet in razor I placed the ga code:
#if (success)
{
<script type="text/javascript">
ga('send', 'event', 'Supplier sign up', 'click', '/signup/success');
</script>
<p>success message</p>
}
The success message appears and I can see the script in the page inspect but I don't get any stats in Google Analytics.
Am I missing something? In the past (a few years ago) I was using a similar code and it worked. Has anything changed recently? Do I need to enable something in the Analytics or my code is wrong?
I am following the documentation:
https://developers.google.com/analytics/devguides/collection/analyticsjs/events
https://developers.google.com/analytics/devguides/collection/analyticsjs/pages
The implementation method for your GA is through gtag.s, not GTM or analytics.js.
Thus you need to modify your success to:
#if (success)
{
<script type="text/javascript">
gtag('event', 'click', {'event_category': 'Supplier sign up', 'event_label': '/signup/success'});
</script>
<p>success message</p>
}
You have mixed two GA libraries. You use gtag.js for page view, so you need to use gtag.js (not analytics.js) for events.
https://developers.google.com/analytics/devguides/collection/gtagjs/events

To get event corresponding to page load time in Google Analytics

I want to track page load time using google analytics. I have a single page application . In order to track i added the given script inside head tag . And in order to get page load time , i added the second script just before the body tag but unfortunately i am not able to find any event in Google Analytics corresponding to this . It seems like this gtag() is not executing somehow. Can someone please help out. Also where do we see event_value in Google analytics. I am able to find event_category and event_action by going into RealTime->Events but i can't find any event_value there.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXX-X');
</script>
<script>
if(window.performance){
// Gets the number of milliseconds since page load
// (and rounds the result since the value must be an integer).
var timeSincePageLoad = Math.round(performance.now());
// Sends the timing event to Google Analytics.
gtag('event', 'timing_complete', {
'name': 'load',
'event_value': timeSincePageLoad,
'event_category': 'Home page loading time'
});
}
</script>
According to documentation it should be value, not event_value
gtag('event', 'timing_complete', {
'name': 'load',
'value': timeSincePageLoad,
'event_category': 'Home page loading time'
});

Google Analytics (gtag) remove own content

Some days ago I introduced the new gtag version of Google Analytics to my private website. Now I try to figure how to filter out my own traffic. Exclusion based on IP is not possible because I enter my website with different browsers from different places. So I wanted to exclude my traffic via cookie. I'm just not able to make it work. The documentation is telling I should work now with dimensions. I tried it but it is not working for me.
I set up a dimension "usertype"
and I added a filter to exclude pattern "internal" for dimension "usertype"
I created a new page for my website and put the following code
<html>
<head>
<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXX-XX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
gtag('js', new Date());
gtag('config', 'UA-XXXXXXX-XX', {'custom_map': {'dimension1': 'usertype'}});
gtag('event', 'kill_ga', {'usertype': 'internal'});
</script>
<!-- Google Analytics -->
<meta http-equiv="refresh" content="5; url=/" />
</head>
<body>
We'll transfer you soon
</body>
</html>
As I said before. I'd like to somehow mark my own traffic as internal and exclude it via filter.
Could someone help me to achieve this please?
There are many approaches to blocking your own traffic, I think using the usertype is one approach. Historically, I've solved it with a simple cookie check:
You'd ask any developer/tester to create a cookie on your site by 1) visiting your site, then 2) entering something like this into their browser's JS console:
Set the persistent cookie, "prevent_ga" equal to true
document.cookie = "prevent_ga=1"
Then check the cookie before invoking GA on your site:
var check_cookie = document.cookie.match(/^(.*;)?\s*prevent_ga\s*=\s*[^;]+(.*)?$/)
if (!check_cookie) {
// do gtag() things
}
I used it finally in a completely different approach.
As you can see on https://www.smest.it I solved with JavaScript directly on my page.
In footer of any page:
<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script>
var gaProperty = 'XXXXXXX-XX';
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
}
</script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXX-XX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){
dataLayer.push(arguments)
}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXX-XX', { 'anonymize_ip': true });
</script>
In my privacy policies, I have the following link.
<a onclick="alert('Google Analytics is now deactivated');" href="javascript:gaOptout()">Deactivate Google Analytics</a>
This will turn off analytics completely.

Resources