Custom Dimension with Universal Analytics - google-analytics

In Universal Analytics
In case I need to set my custom dimension on page load with a single pageview, should I need to set it prior to send.
ga('set', 'dimension1', dimensionValue); ga('send', 'pageview');
Let me know if this works.
Thanks

Yes, that would work.
There is also an alternative syntax where you can set the dimension with the page view call:
ga('send', {
'hitType': 'pageview',
'dimension1' : 'dimensionValue'
});
The important thing is that there must be some kind of interaction event (pageview or event) to transmit the transaction value.

Related

Cookies Consent - re-sending page view event?

I am not sure about Google Analytics behaviour in sending allowAdFeatures.
I added analytics.js the default way, setting only the tracker:
<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-XXXXXX-XX', 'example.com');
ga('send', 'pageview');
</script>
When the user lands on my site, I show him a popup asking him to approve saving cookies.
Upon his approval I send:
ga('set', 'anonymizeIp', false);
ga('set', 'allowAdFeatures', true);
My question is about the current page that he is viewing (before approving the consent) - will the action of page view be able to be re-targeted for that current user or I need to send another event of page view?
In your case you need to send another page or an event with the new settings in order to send the updated data to Analytics.
However, you don't need to send this with your second hit:
ga('set', 'anonymizeIp', false);
Rather this with the previous pageview (and nothing with the second):
ga('set', 'anonymizeIp', true);
anonymizeIp is not active by deafult, so you must declare it in the initial code.

Sending event to specific property

I am trying to send an event to a specific property on a page. The page has multiple properties on it and I only want to raise this event in one specific property.
I have used ga.getAll() to see the properties on the page and the one I want is labeled gtm3 so this is code I am trying:
ga("gtm3.send", {
hitType: "event",
eventCategory: "Heartbeat",
eventAction: "Beat",
eventLabel: "Heartbeat",
nonInteraction: true
});
This does not work, debug mode gives me the message:
command ignored. unknown target undefined
What did I do wrong?
I am testing this in console also so the tag is definitely registered. Am I referencing it wrong? In the getAll() I can see the property name: "gtm3".
I am running this code on doc ready but have also tested direct in console so I don't think it is a timing issue. What is proper way to reference this property and reference an event?
edit:
I also get the same message when I just run a simple: ga('send', 'pageview');
Why is target undefined?
OP was using Google Tag Manager as the method for GA implementation. Suggested to use DataLayer events to send the heart-beat event to GTM and utilize triggers within GTM to send the heart beat event to the appropriate GA property.
DataLayer Trigger on page:
....
DataLayer.push('event':'heart-beat');
....
The above is captured within GTM in a Custom Event trigger.
The Custom Event trigger is then used on a GA Event Tag set to non-interaction.

Event_name parameter in gtag.js

I am trying to migrate from Universal Anaylics to the newer Global Site Tag. I wish to migrate this code:
ga('send', 'event', 'MyCategory', 'MyAction', 'MyLabel', 1);
to:
gtag('event', '[EVENT_NAME???]', {'event_category':'MyCategory', 'event_label': 'MyLabel','event_action': 'MyAction','value':1});
I read https://developers.google.com/analytics/devguides/collection/gtagjs/events#recommended-events but I do not understand what the value for the event_name parameter should be and what the purpose of this value/parameter is.
The event name in gtag defaults to the event action in GA. If you use the "recommended event names" as provided by Google the gtag code will automatically insert category and label with predefined (by Google) values (which you may override). You can however also use custom event names (so you need only specify category and label in the JSON).
In the larger scheme of things this is Google's attempt to unify web tracking and mobile tracking, since the suggested events for gtag.js match the recommended events for Firebase mobile tracking.

Google Analytics hitCallback for every hit?

Is it possible to set a global hitCallback in Google Universal Analytics:
So any hit, calls the same callback?
Without modifying each ga('send'..
You can't set the hit callback globally, but you can set it on the tracker object itself, which means it'll get called for every hit that tracker sends (which is probably just as good as globally, for your purposes).
Here's an example:
ga('create', 'UA-12345-1', 'auto');
ga('set', 'hitCallback', function() {
console.log('hit is done');
});
ga('send', 'pageview');
ga('send', 'event', 'Button', 'click');
If you run this code, you'll see "hit is done" logged to the console twice.

How to track conversions without a confirmation page

I have a form on my site that, upon a successful completion, triggers a confirmation message in a modal. I need to be able to track the conversions on this form. I think I need an event tracker here, but I can't figure out where I'd put it.
The form is at http://www.nearlynewlywed.com/a/sell
The best practice is to create a virtual page view that will simulate a "thank you" page that will trigger the goal.
Execute this JS code when the form is validated and submitted:
_gaq.push(['_trackPageview', '/your-directory/form/thank-you']);
If you use Universal Analytics, then:
ga('send', 'pageview', '/your-directory/form/thank-you');
Now the thank you page will be rendered as a pageview, which will appears on your reports and Goals' funnel visualization. Remember to set the "/your-directory/form/thank-you" as the goal destination on Google Analytics.
If I understand your question correctly, you should push and event to GA when your form is validated and it passes. That means, if every field is ok, then at the same time you send the command to open the modal window you send a ga tracking command like this.
_gaq.push(['_trackEvent', 'Form', 'Success', 'Form x was completed']);
You could do something like this with jQuery
$('#formId').on('submit', function(){
_gaq.push(['_trackEvent', 'Form', 'Success', 'Form x was completed']);
});
OR add this to your form HTML tag
<form action="action.php" onsubmit="_gaq.push(['_trackEvent', 'Form', 'Success', 'Form x was completed'])">

Resources