Can I capture div#Id and put in Google Analytic Event - google-analytics

Can I do like this
var eLabel = $('#example_placeholder').html();
and put eLabel into this
ga('send', {
'hitType': 'event', // Required.
'eventCategory': 'cash', // Required. privilege acquisition creditcard installment
'eventAction': 'ClickView', // Required.
'eventLabel': eLabel
});

Yes, you can do that. You can shorten things like this as well:
ga('send','event','cash','ClickView',$('#example_placeholder').html());

Related

Migration from ga to gtag.js scripts

Couldn't find direct migration tutorial or clear description on https://developers.google.com/gtagjs/reference.
Have limitation to analitic portal access and can't properly to test it.
Need to change next ga scripts under my SPA app to gtag.
1. ga('set', 'page', url)
2. ga('send', 'pageview')
3. ga('send', 'event', 'button-interest-request', 'submit-request')
I didn't find how should works set on gtag.
I supposed should be something like next strings for strings 1 and 2
gtag('event', 'page_view', { page_location: url })
And next for string 3
gtag('event', 'button-interest-request', {
'value': 'submit-request'
});
Am I right?
You have to define at least event category and event action in gtag with this syntax:
gtag('event', <action>, {
'event_category': <category>,
'value': <value>
});
In your situation, like this:
gtag('event', 'submit-request', {
'event_category': 'button-interest-request'
});

Google Analytics Universal Tracking Events: not tracking

just want to check to make sure I have the correct syntax with the event tracking...
ga('send', {
'hitType' : 'event',
'eventCategory' : 'links',
'eventAction' : 'click',
'eventLabel' : 'sidebar-link',
'nonInteraction' : 1
});
This is used for links that open in a new tag. Also I've found that for links that do not open in a new tag, you can add the member:
'hitCallback': function() { document.location = 'http://link-to.com'; }
Is this all looking correct? Because I'm not receiving any tracking events on my pages. I checked in debugger, and the code is being called for sure, but nothing is coming up in GA. What's up?
I dont believe you have the correct syntax. Try this syntax
ga('send', 'event', 'button', 'click', 'nav buttons', 4); //USE THIS
ga(send, event, eventCategory, eventSction, eventLabel, eventValue) // VARIABLE NAMES
**** You do not need to include the event value parameter. All other event parameters are recommended.
Gonna answer my own question here as I got it working - also I want to provide an example for the alternate syntax, for those who might be confused about it like I was. **Edited for clarity
First off, it was a matter of waiting for a day for the results to show up, even in "Real Time" mode.
Second, this is what I went with:
For links that open in new tab:
//HTML
<a class='newtab' data-type='label-name' href='http://blah.com' target='_blank'>Link to blah</a>
//JS
$('.newtab').click(function(){
var label = $(this).attr('data-type');
ga('send', 'event', 'category-name', 'click', {
'eventLabel' : label,
'nonInteraction' : 1
});
});
For links that open in same tab:
//HTML
<a class='sametab' data-type='label-name' href='http://blah.com'>Link to blah</a>
//JS
$('.sametab').click(function(){
var linkTo = $(this).attr('href');
var label = $(this).attr('data-type');
ga('send', 'event', 'category-name', 'click', {
'eventLabel' : label,
'nonInteraction' : 1,
'hitCallback' : function() { document.location = linkTo; }
});
return false;
});
The part that threw me was in the example where it showed how to add either all attributes in an object or some attributes in the object, and which attributes were available. Anyway, voila :)

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');
});

Is it possible to associate events with campaigns in Google Analytics (Universal)

We're using events to track impressions and clicks on campaign elements (carousel images, sidebar ads and footer banners). We're trying to associate each of those events with a Campaign so that we can report on campaign-specific events. It's not working. Events are created, but they are not associated with a campaign.
The documentation for events using analyitcs.js (https://developers.google.com/analytics/devguides/collection/analyticsjs/events#implementation) seems to suggest that we can add additional attributes to events using the Field Reference: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference
Our event creation code is as follows:
$('#campaignImage').on('click', function() {
ga('send', {
'hitType': 'event',
'eventCategory': 'Promotions',
'eventAction': 'Click',
'eventLabel': 'IMAGE_TITLE',
'page': window.location.pathname,
'campaignName': 'CAMPAIGN_NAME'
});
});
Events are created successfully, but are not associated with the specified campaign (they all show up with campaign as "not set"). Is it even possible to do what we're trying to do, or is it only possible to track traffic acquisition for campaigns using URL parameters?
UPDATE - SOLUTION BELOW
Based on a recommendation from Blexy
We switched to use Advanced eCommerce...setup described here
Our code, simplified:
$( document ).ready(function() {
//Promotion clicks
$('.promo-img').on('click', function() {
ga('ec:addPromo', {
'id': $(this).attr('data-campaign'),
'name': $(this).attr('data-campaign'),
'creative': $(this).attr('data-unitname'),
'position': $(this).attr('data-position')
});
ga('ec:setAction', 'promo_click');
ga('send', {
'hitType': 'event',
'eventCategory': 'Internal Promotions',
'eventAction': 'Click',
'eventLabel': $(this).attr('data-unitname'),
'pageview': window.location.pathname
});
});
});
$(window).load(function(){
//Promotion impressions
if ($('.promo-img').length > 0) {
ga('ec:addPromo', {
'id': $('.promo-img').attr('data-campaign'),
'name': $('.promo-img').attr('data-campaign'),
'creative': $('.promo-img').attr('data-unitname'),
'position': $('.promo-img').attr('data-position')
});
}
ga('send', 'pageview');
});
I don't believe what your attempting is going to work, as the event creation code is only going to accept its specific parameters.
However, I do think what you're trying to do is possible using Enhanced Ecommerce - Measuring Internal Product Promotions.
For example, you could pass this event when an ad is shown:
ga('ec:addPromo', { // Promo details provided in a promoFieldObject.
'id': 'PROMO_1234', // Promotion ID. Required (string).
'name': 'Summer Sale', // Promotion name (string).
'creative': 'summer_banner2', // Creative (string).
'position': 'banner_slot1' // Position (string).
});
And this click code when an ad is clicked:
// Identify the promotion that was clicked.
ga('ec:addPromo', {
'id': 'PROMO_1234',
'name': 'Summer Sale',
'creative': 'summer_banner2',
'position': 'banner_slot1'
});
// Send the promo_click action with an event.
ga('ec:setAction', 'promo_click');
ga('send', 'event', 'Internal Promotions', 'click', 'Summer Sale');

Is it possible to track custom metrics on interaction events instead of on pageview?

I would like to create the custom metric "rating" and send it on a click event along with a rating value. What am I doing wrong?:
var ratingValue = '5';
ga('send', 'event', 'button', 'click', 'rating', {
'nonInteraction': 1,
'page': window.location.href,
'rating': ratingValue
});
Reference to docs:
https://developers.google.com/analytics/devguides/collection/analyticsjs/events
You need to do two things:
In Google Analytics, create a new Custom Metric (admin > custom definitions > custom metrics).
In your event, specify the metric you are sending to rather than the name of the metric:
var ratingValue = '5';
ga('send', 'event', 'button', 'click', 'rating', {
'nonInteraction': 1,
'page': window.location.href,
'metric1': ratingValue
});

Resources