Hard code GA event with GTM - google-analytics

I want to be able to hard code some GA event such as below. I'm using GTM and I understand its not possible in this way. Is there a way round this?
ga('send', 'event', 'Mobile', 'Original', 'App');

This is a problem because GTM creates a randomly named tracker instead of the default tracker (t0). You can either use the set fields method on the "name" field to set the tracker name to a known values (i.e. "myTracker") and adapt your calls accordingly:
ga('myTracker.send', 'event', 'Mobile', 'Original', 'App');
Or you can use the ge function to send your event tracking calls to all trackers in the page:
ga(function() {
var trackers = ga.getAll();
for (var i=0; i<trackers.length; ++i) {
var tracker = trackers[i];
tracker.send('event', 'Mobile', 'Original', 'App');
}
});
which will probably create more headache than it's worth. However it is unlikely that there is a scenario that can't be covered without a need to hardcode events - the proper way would be to push a custom GTM event (and your GA event data) to the dataLayer and trigger an GA event tracking call from there.
So for hardcoding event, just don't.

To implement ga event use this syntax wit your onclick.
replace ga('send', 'event', 'Mobile', 'Original', 'App');
with this and it will work:
gtag('event', 'click', {'event_category' : 'Mobile',
'event_action' : 'Original', 'event_label' : 'App'});

Related

Google Analytics 4: custom dimensions in gtag.js

I am trying to send some custom dimension together with my event.
This is my code:
gtag('config', 'G-XXXXXXXXXX', {
'custom_map': { 'dimension1': 'age' }
});
gtag('event', 'age_dimension', {
'event_category': category,
'event_label': action,
'value': data(),
'nonInteraction': 1,
'age': '666'
});
This is my dimension on UI:
I am sending my events together with my custom dimension, on UI I see all my data as parameters in the event,
but when I try to make something with my dimension GA just says me that there is no data for this dimension.
I don't know, probably I configured the map incorrectly or probably something wrong with the dimension which I created.
So the question, is my configuration and dimension correct? and if no, what is wrong? thanks!

google analytics not tracking custom dimension in context to events

I need to differentiate the users (Internal and External) who are performing some event action. so I am using the following code to track the user using custom dimension. My end goal is to know who performed the specific event by internal or external user
ga('send', 'event', 'Contract Change', 'click', 'Landing Page', 'My Value', {
'dimension3': 'External'
});
ga('send', 'event', 'Contract Change', 'click', 'Landing Page', 'My Value', {
'dimension3': 'Internal'
});
I have triggered this tracking code from javascript console for the testing purpose however it's not logging any data and I am not able to get the custom dimension value at any of the place.
I would change a couple of things:
Integers for event value: like Eike Pierstorff said, the event value has to be an integer. Technically you could leave the field out as it's optional, but it's best practice to assign values to your goal, so you can quantity the importance of user actions on your website:
https://developers.google.com/analytics/devguides/collection/analyticsjs/events#event_fields
Set dimensions before events: what you're doing should work, however it makes the code harder to read (most code formatting will break the {} syntax into 2 lines). Also, if you're sending multiple events which should be associated with the dimension, it's just cleaner to have the dimension declaration first, then all your events. Finally, if it's a user-scope dimension (internal vs. external users), it's more logical to declare the dimension before events, otherwise it looks like the dimension is hit-scope:
For instance:
ga('set', 'dimension3', 'Internal');
ga('send', 'event', 'Contract Change', 'click', 'Landing Page', 10);
ga('send', 'event', 'Other event with dimension', 'foo', 'bar', 20);

How do I set appVersion for Google Analytics Event tracking

When I try to set the appVersion in google analytics, then my event tracking stops working. Specifically, I'm trying to include the app version with event tracking so I can know which version of the app caused an event.
I've tried setting the app version like this:
ga('set', 'appVersion', app.version);
I've tried like this:
ga('send',
'event',
eventCategory,
eventAction,
{'page': pageName, 'appVersion' : app.version });
And I've also tried the alternative syntax:
ga('send',
{'hitType' : 'event',
'eventCategory' : eventCategory,
'eventAction' : eventAction,
'page' : pageName,
'appVersion' : app.version});
If I include appVersion, then event tracking stops working. No events show in realtime and no show the next day in the Behavior/Events section. The PageViews still work though.
As requested in the comments, I am editing to add in my event tracking code. It's been through several variations while I tried solve this problem. Here's what it looks like currently.
var app = {
trackEvent: function (pageName, eventCategory, eventAction, optionalEventLabel, optionalEventValue) {
var eventObject = {
'eventCategory' : eventCategory,
'eventAction' : eventAction,
'optionalEventLabel' : optionalEventLabel,
'optionalEventValue' : optionalEventValue,
'page' : pageName,
};
console.log("app.trackEvent - " + JSON.stringify(eventObject));
ga('send', 'event', eventObject);
}
}
I call this method from many places using a call like:
app.trackEvent("PageNameValue", "EventCategoryValue", "EventActionValue", "EventLabelValueIfIHaveOne", AnIntegerValueIfIHaveOne);
Any help or suggestions will be greatly appreciated.
Edit...
I found the following bug report that seems to apply: https://code.google.com/p/analytics-issues/issues/detail?id=366
The bug reporter mentions solving this problem by setting up a custom dimension. I will give that a try.
This appears to be be a Google Analytics bug. See https://code.google.com/p/analytics-issues/issues/detail?id=366 for more information.
As suggested by the bug reporter, the workaround is to use a custom dimension that you define in the Admin / Custom Definitions / Custom Dimensions section of the Google Analytics console.
Click "New Custom Dimension"
Enter name ( I entered customAppVersion )
Choose scope ( I chose Hit )
Click Create
Google will then suggest code examples for you, like...
var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension1', dimensionValue);
The only thing in the code sample that you need to change is the value of 'dimensionValue'. So I ended up with the following code.
ga('create', 'UA-########-#', 'auto');
ga('set', 'checkProtocolTask', null); // Disable file protocol checking (so that GA will work on Android devices)
ga('set', 'dimension1', app.version);
ga('send', 'pageview');
After this, the custom dimension will be applied to each hit recorded by Google Analytics and you can use that custom dimension to filter your results in the Google Analytics console.
As per google
Since the appName field must be sent with all app hits, it's often
best to set that field on the tracker itself using the set command or,
alternatively, when the tracker is created:
ga('create', 'UA-XXXXX-Y', 'auto', {
'appName': 'myAppName'
});
// The `appName` field is now set on the tracker, so
// screenview hits don't need to include it.
ga('send', 'screenview', {appVersion: '1.2'});
// Sending multiple parameters
ga('send', 'screenview', {appName: 'com.company.app', appVersion: '1.2'});
More information here
It works if you set at least the "appName", it's a good practice to set "appName" and "appId" before to set "appVersion"
ga('set', 'appId', app.id);
ga('set', 'appName', app.id);
ga('set', 'appVersion', app.version);

Social shares tracking in GA

This is pretty much covered topic for original FB/Twitter buttons. But what if I have my own "share on fb" button? Like this:
<div id="fb_share"><a target="_blank" href="http://www.facebook.com/share.php?u=blah-blah">Share on FB</a></div>
so I've come up with the folloing solution:
var FBbtn = document.getElementById("fb_share");
FBbtn.addEventListener('click', function() {
ga('send', 'social', {
'socialNetwork': 'facebook',
'socialAction': 'share',
'socialTarget': window.location
});
//console.log('tracked');
});
That is placed AFTER the Google Analytics code.
Despite the fact it wont catch FB callback - it is supposed to do the trick but for some reason I still cannot see any results in Analytics so the question is this: will the solution actually work? In fact it could be even like this I believe:
FB
Your 'share on Facebook' links causes the page to navigate (and not open a new window/tab). When this navigation happens, most mainstream browsers cancel all pending HTTP requests for the current page and then navigates to the new page (fb.com)
In this scenario, one of the pending HTTP requests will be the GA event tracking call which will therefore never complete and never be received by the GA servers.
What you need to use is the GA hit callback functionality, this essentially cancels the native navigation (to FB), sends the tracking call and waits enough time for it to complete and then does a JavaScript redirection to the next page.
You should read the google docs here
In your case your event tracking function should be similar to this:
var FBbtn = document.getElementById("fb_share");
FBbtn.addEventListener('click', function() {
ga('send', 'social', {
'socialNetwork': 'facebook',
'socialAction': 'share',
'socialTarget': window.location,
'hitCallback': function(){
window.location = this.href;
}
});
//console.log('tracked');
return false;
});
So I've made the following changes:
Added the hitCallback property to the event tracking call. this is an anonymous function that is called once the GA servers have sent their response to the event tracking.
added a 'return false' statement which cancels the native functionality and then relies on the hitCallback function to do the navigating.

Getting events to be dependent on the completion of another event

A part of my site generates a handful of events from the same action. The default for Google Analytics is to have the events run independently. However, one of the events needs to happen last. Is there a way to force an order of event completion or at the least have an event dependent on another event?
I have found no documentation on the subject. Thanks in advance for the help.
Could you do a simple flag?
Event #1
var flag = false;
function eventOne(){
ga('send', 'event', 'Event Chain', 'Trigger', 'First Event');
flag = true;
}
function eventTwo(){
if (flag) {
ga('send', 'event', 'Event Chain', 'Trigger', 'Last Event');
}
}
Google Universal Analytics has hit callbacks , functions that are called when a tracking call is completed. You can at least try to nest event tracking calls within event hits
ga('send', {
'hitType': 'event', // Required.
'eventCategory': 'category', // Required.
'eventAction': 'action', // Required.
'eventLabel': 'label',
'hitCallback': function() {
ga('send', {
'hitType': 'event', // Required.
'eventCategory': 'category', // Required.
'eventAction': 'action 2', // Required.
'eventLabel': 'label 2'
});
}
});
Untested, but judging from the documentation I don't see why this wouldn' work.
There is no built-in way of doing that in Analytics. To implement this sort of behavior, you would have to design it yourself based on your requirements and/or application logic.
If you are implementing a website using JavaScript, you might be interested in using the q library, which could help you in creating and composing asynchronous promises in JavaScript.
Without going into too many details (because it might be out of the scope of your question), that could yield something like:
Q.all([handleEventOne(), handlerEventTwo()]) // Wait for 2 handlers to complete
.done(function (values) { // Execute this callback once the 2 handlers have completed
ga('send', 'event', 'Event Chain', 'Trigger', 'Last Event');
});

Resources