What is the correct syntax to set a custom dimension in Google Analytics? - google-analytics

I'd like to send some custom dimensions back to GA with every blog post that is read on my website - author and category.
When I set up the custom dimensions in GA I got this code to use:
var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension2', dimensionValue);
However, in the GA docs it specifies a different syntax using "send" rather than "set".
https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
ga('send', 'pageview', { 'dimension4': '<?=$categories?>'});
We are using universal google analytics, but this conflicting information means I'm not sure which syntax to use.
Thanks in advance

The first code block is how you set the custom dimension (CD) that can be sent with any hit: event, pageview, transaction, etc.
The second code block is how you set the CD and send it with a pageview. So it's a specific example of the first method.
Both are valid, it's just that the second example is more complete.
When sending data to CDs, don't forget to create and define them in the GA configuration as well.

Related

Which is the best method to set up event tracking for new account signup steps in GA4?

Currently I'm using the following analytics.js method to track different funnels/goals and these get triggered on each specific page.
ga('send', 'pageview', '/ConversionEvents/Step1_MembershipPlan');
ga('send', 'pageview', '/ConversionEvents/Step2_AccountInformation');
ga('send', 'pageview', '/ConversionEvents/Step3_PaymentInformation');
ga('send', 'pageview', '/ConversionEvents/Step4_AccountConfirmation');
ga('send', 'pageview', '/ConversionEvents/Step5_ThankYou');
However, now that I switched to gtag.js I'm seeing what the best method would be to set these up. I came up with the following 3 options.
Option 1:
gtag('event', 'sign_up', {'step_name': 'step1_membership_plan'});
Option 2:
gtag('event', 'pageview', {'step_name': 'step1_membership_plan'});
Option 3:
gtag('event', 'step1_membership_plan');
I'm leaning towards using option 3 because it would be easier to view these events in GA4 since it will be listed as a different event name and it would be easier to create funnels.
From reading online, they say it's better to use the recommended/automatic events like sign_up and pageview for this but I'm not sure if then I can set up events to track the different parameters (e.g. step_name) that I set up for each step.
Which option would be the best to go with?
Thank you!
Personally, I would not clutter my GA4 with too many distinct event names.
But that's a matter of taste: If I understand Google's limits page correctly, web properties do not currently have a limit on distinctly named events, whereas app properties are limited to 500.
Ref.: https://support.google.com/analytics/answer/9267744?hl=en)
Instead, I would send the following events:
sign_up, when a user has completed the registration, aka visits the thank you page.
sign_up_step_complete, when a user has completed a step in the registration process. Sending the custom dimension step_name for each step.
(That is assuming that each step is just a virtual page view and does not actually send a page_view event to Google. If it does send a page_view, you can create the sign_up_step_complete event server-side based on the URL or not create an event at all and just use the page_view.)
This way, you're using the dafault sign_up event which might show up in a default GA report while also tracking intermediate steps in a separate custom event.
Finally, I would visualize the data in a free-form exploration funnel analysis.

Google Analytics Global Site Tag Custom Parameters

According to Google's gtag.js guide, it seems like we're able to define custom parameters. However, when using the code examples, only the Event Action gets populated. The Event Label is recorded in Google Analytics as "(not set)" and the Event Category as "general".
Code Example from developers.google.com:
gtag('event', 'video_play', {
'video_title': 'My promotional video',
'duration': '01:32'
});
It's also interesting to note that I cannot figure out how to show custom parameters as the columns in Google Analytics seem to be statically set to "Event Category", "Event Action", and "Event Label". These correspond to the default keys of "event_category", "event_action", and "event_label". Using these keys sends the values correctly. The following code works:
gtag('event', 'redirect', {
'event_category': 'Announcements',
'event_label': '/announcements/index.jsp',
Has anyone gotten custom parameters to work or is this a feature that hasn't been implemented yet in gtag.js? Is there additional configuration needed that I may have missed?
If you you were thinking of GA Custom Dimensions and Custom Metrics, yes it is available in the gtag.js / Global Site Tag syntax, see
https://developers.google.com/analytics/devguides/collection/gtagjs/custom-dims-mets
in the form of a Map of CD indexes and attribute explicit names, followed by setting values to explicit attribute names.
for example
// Maps 'dimension2' to 'age'.
gtag('config', 'GA_MEASUREMENT_ID', {
'custom_map': {'dimension2': 'age'}
});
// Sends an event that passes 'age' as a parameter.
gtag('event', 'age_dimension', {'age': 55});
See also https://developers.google.com/analytics/devguides/collection/gtagjs/migration#custom_dimensions_and_metrics
However, gtag.js is a wrapper to make analytics.js easier to implement by hiding some its complexity.
If you are used to analytics.js, keep using it, you get more control on its behavior.
Or move to GTM, it's way more flexible.

Two Google Analytics Codes - both not collecting correctly

I have inherited a site which has two GA-UA codes.
These are setup like this
// first code
ga('create', 'UA-XXXXXXXX-1', 'auto');
ga('send', 'pageview');
//second code
ga('create', 'UA-XXXXXXXX-1', {'name':'second'});
ga('second.send', 'pageview');
I have no idea why they are setup like this and indeed do not understand the syntax of the second tracking code
The following can be observed
THE FIRST CODE
Custom events working and being successfully collected
Linked successfully to Adwords
Only has a few months of data and therefore we don't want to use this
THE SECOND CODE
Does not record custom events
Can't link to Adwords
Has years of data attached to it that we want to use
The plan is to just use the second code
For complete brevity custom events are being triggered like this
ga('send', 'event', 'Contact', 'contact-form', 'Goals');
My questions is this
"Because it has lots of historical data we wish to use code two but it is not collecting custom events. How do I get the second code to collect custom events so we can retire the first code? "
If you use two trackers on one site you need to give a name to at least one of them, else the first tracker is overwritten when the second one is initialized.
Thus the syntax for your second piece of tracking code - when the tracker is initialized it is assigned a name, in your example "second".
If you want to send data to that tracker you need to prepend the assigned tracker name to the send call, in your example:
ga('second.send', 'event', 'Contact', 'contact-form', 'Goals');
That is why the second tracker does not receive your event tracking calls, they are only being sent to the first "unnamed" tracker (actually if a tracker name is not assigned it defaults to t0).
Also do not think you can link an Adwords account to two different Google Analytics accounts, so. you'd need to unlink the first account and then link the second one.

Custom reports are empty in Google Analytics receiving data from Google Tag Manager

I want to create a custom report with custom dimensions and custom metrics, so I've created them as admin in GA and both have index 1.
I've added them in the dataLayer in my code:
var dataLayer = {'dimension1':'Custom Dimension 1','metric1':123456}
And after setting in GTM, I've checked data is being sent right in live mode:
But custom reports are empty, but activity on site is being registered.
This is the entire code of my site. I have tried adding both Google Tag Manager and Google Adwords codes but does not work in any case:
<body>
<script>
var dataLayer = [{
'dimension1':'My Custom Dimension 1',
'metric1': 123456
}]
</script>
<!-- Google Tag Manager -->
<!-- End Google Tag Manager -->
<h1>Nuevo alojamiento</h1>
<script>
//GA CODE
ga('create', 'UA-76206495-2', 'auto');
ga('send', 'pageview');
</script>
</body>
Pushing those values with those standard reference names in to GTM will not associate them with your hits, or make them show up in your reports. In GTM you need to grab the value of your CD and CM from the dataLayer. In this case you need to create dataLayer type variables that will give you access to dimension1 and metric1, although I would advise to rename them to something else other than the standard GA reference names, ie. dimensionX and metricX because that could be confusing. Then you need to pass the index and value for that CD and CM into whichever tag (page view, event, etc.) using those new names.
Edit: note that data won't show up in your standard reports until they have been processed, which takes up to 24 hours or so.
As nyuen told me, I have to set those values in GTM, it was done bescause, if it wouldn't, data would not been sent. These are my settings
This sends the CD and CM in all pages. Custom vars are already set.
Preview mode of CTM displays this, it was fired:
And de dataLayer contains the data (I'm going to follow you advice and use another name):
So if I leave the preview mode and publish in GTM, I'm sure information is being sent, that is the GET request I placed before. Those values era in the URL as params (cd1 and cm1), so that is the reason why I'm pretty sure information is being sent to GA
Hope this extra information helps
UPDATE 1
I've changed dataLayer values to dim1 and met1 and now met1 is set to 123456789. Also, verified UA is right.
With those changes, chain sent to Google Analytics is the same (params are cd1 and cm1 with the appropiate values)

How do I pass a parameter from my site into google analytics?

Let's say I have a site and the user comes into it with a parameter:
http://example.com&url=blahblahblah
How do I go about passing along the url value from the parameter into Google Analytics?
1) User comes to the page with a url in the params
2) User clicks a download link with a ga tracking code attached to it which was generated from ga account like this:
http://example.com/download/param1=dkljdf&_ga=1.149898996.39207121.1424368466
You have to create a custom Dimension and a metric for that.
About custom Dimensions and metrics:
https://developers.google.com/analytics/devguides/platform/customdimsmets
After you have created a Dimension, you can add metrics to it by view, in example.
Follow the steps here for Universal Analytics:
https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
Note that due to not have 10 point of reputation, I wrote the a "_" in http like "ht_tp"
BUT:
I think what you want to know is the number os visitors that clicks a download link in your site that comes from, lets say "blahblahblah" as web origin or other methods.
For that, you have the param utm_source that you can receive directly in the url.
So instead of ht_tp://example.com&url=origin you should receive ht_tp://example.com&utm_source=origin
In this way, you have no care about it. Analytics is going to take care for you so you can get a report of clicks by source.
Or, just use the referer in case all the incoming visitors are from webs:
ga('set', 'referrer', 'ht_tp://example.com');
And a final option, to use Events:
_gaq.push(['_trackEvent', 'ReferencedVisitors', jsVarWhereYouHaveTheOrigin]);

Resources