Using GA4 property with (gtag.js) following config:
gtag('js', new Date());
gtag('config', 'G-XXXXXXX', {
client_storage: 'none',
client_id: clientId, // I generate this
anonymize_ip: true,
});
And it still writes _ga and _ga_XXXXXXX cookies.
From what I found the flag client_storage=none only works for the old UA- properties, but for the new G- properties it doesn't have any effect.
The new way when using gtag is:
gtag("consent", "default", {
ad_storage: "denied",
analytics_storage: "denied",
functionality_storage: "denied",
personalization_storage: "denied",
security_storage: "denied"
});
The first two storage settings are documented here in the tag manager documentation and this support ticket mentions some additional settings that can be denied.
I tested this and it doesn't set a cookie any longer.
Apparently it breaks real-time analytics, but I do see regular data comming in after a day as usual.
The new G- properties it always has the data anonymized by default, so there is no need for the parameter anonymize_ip.
Anonymization tends to be used to assimilate the data from statistical to technical and to be able to track with Google Analytics.
Blocking tracking is not a good practice, inconsistent and unrepresentative data can be collected based on when the user accepts the cookie policy.
That kind of configuration (client_secure) works in UA while in GA4 doesn't work (at least currently). It is still an evolving system.
I have a react project and I call
ReactGA.gtag('consent', 'default', {
ad_storage: 'denied',
analytics_storage: 'denied',
functionality_storage: 'denied',
personalization_storage: 'denied',
security_storage: 'granted',
wait_for_update: 2000,
})
before the ReactGA.initialize method and it seems there are no cookies in the developer console. I don't see much into this. More context here
EDIT: Even tho the setting remove the cookies, it turned out the GA stopped tracking, so basically nothing worked.. :/
Related
Maybe I'm missing something here, but in Google Analytics V3 (analytics.js or gtag.js) setting the transport or transport_type to beacon would make the request to google-analytics.com/x/collect be sent using POST, i.e. no GET-parameters would be shown in the request URL.
When using GA4 (and a G- measurement ID) setting this parameter does nothing. This is what my tracking code looks like:
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXX', {"transport_type":"beacon"});
Am I doing something wrong, or am I simply misinterpreting how the beacon works in GA4? If the latter, is there any way to make GA4 function in a similar manner? What I'd like to achieve is send requests, that look like:
https://www.google-analytics.com/g/collect
As opposed to:
https://www.google-analytics.com/g/collect?param=1¶m_2=2&etc=etc
Hopefully someone'll be able to help me out, as the documentation is very limited ATM.
In Firefox in Network section of developer tools you can see the initiator of hit, and it say the way how hit was sent. If you see something like js.56 (beacon) you can be sure that hit was sent via navigator.sendBeacon() method of Beacon API
Setting parameter transport_type to beacon indeed do nothing, because ga4 hits are sent using this method by default.
I followed the directions given when setting up the User-ID option.
I then created a view named 'user_id' for showing user_id info, but I am not seeing anything. When enabling User-ID, the code said to add the following to my tracking code.
gtag('set', {'user_id': '12345UserIdHere'});
However, when I go to the 'user_id' view, nothing is shown.
My current goal is to view which user ids were browsing the system during a given period of time.
Edit:
I am using this library:
https://www.googletagmanager.com/gtag/js?id=MY-ID-STUFF
I mostly used GTM, so I am not completely familiar with the gtag.js syntax, but I don't believe it has a "set" method. According to the documentation you'd have to set the user id in the "config" call instead:
gtag('config', 'GA_MEASUREMENT_ID', {
'user_id': 'USER_ID'
});
(Things in caps are placeholders).
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.
Situation
On our site we implement virtual pageviews for ajax filters and page scrollings.
When user open Rent page on our site:
real url: "/rent"
send to GA:
ga("send", "pageview", {page: "/rent/"})
When user scroll to second page:
we change real url via js: "/rent/?page=2"
send to GA (we care only about main page):
ga("send", "pageview", {page: "/rent/"})
Or when user drills down:
we change real url via js: "/rent/appartment/?page=2"
send to GA (we care about path):
ga("send", "pageview", {page: "/rent/appartment"})
But we noticed that really GA code does not take current real url (window.location) each time we call ga, but uses first time location. So when user drill down GA code send:
GET collect?...&t=pageview&dl=http://example.com/rent&dp=/rent/appartment&...
....
Referrer: http://example.com/rent/appartment
Here dl parameter is location of our page (not really current location) and dp is a page parameter in ga call. Notice that referrer is ok.
We decided to provide ga with real location (like in referrer) and change code to:
ga("send", "pageview", {page: "/rent/appartment", location: "http://example.com/rent/appartment"})
Problem
From now on I was satisfied, but was not guys that uses GA to analyze Paid Search effectiveness: bounce rate raised drammaticaly (something like 20% to 70%).
Drilling to the problem I've noticed that GA "loses" user when we remove campaign parameters (utm...) and send location without them.
Questions
Should I care about real location in this situation? How does location affects pageviews? How can I workaround or solve this problem?
Additional information
Seems like the main problem is that we drop CPC parameters when we change location for GA:
landing page: /rent?utm_source=...&... (or gclid for Google)
scroll down to second page: /rent?page=2 - there are no CPC parameters.
Additional information here: https://support.google.com/analytics/answer/1714454?hl=en
But I'm still can't figure out an appropriate solution.
When you send a hit to GA with analytics.js, it's going to send the data it has stored on the tracker object that you created when you invoked ga('create', ...);. You can override those values by passing in an object (as you did), but if no overrides are specified, that tracker data is used.
When the tracker object is created it collects data about the current page (e.g. url, title, window size, etc) and stores that information. It does not recollect that info before you send hits. You have to update it if that data changes.
That means if you're updating the page information in an AJAX site and want analytics.js to "remember" that you've updated the page, you'll have to set it on the tracker. The advantage to setting it on the tracker is that if you send other hit types (e.g. events, social, exceptions), you won't have to specify those new values each time.
So, instead of doing:
ga("send", "pageview", {page: "/rent/appartment"})
Do this:
ga("set", {page: "/rent/appartment"});
ga("send", "pageview");
Now, if you send an event later, the event will be associated with the apartment page.
UPDATE: there is now an official guide for how to properly track single page applications with Google Analytics.
I need to split collecting data for two GA accounts, let`s name them UA-XXXXXXX-1 and UA-XXXXXXX-2. To implement this, I used example code from https://developers.google.com/analytics/devguides/collection/gajs/ (under "Pushing commands to multiple trackers also works" text) and here is my code:
_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setCustomVar', 1, 'customVar1', 'cv1', 1]);
_gaq.push(['second._setAccount', 'UA-XXXXXXX-2']);
_gaq.push(['second._trackPageview']);
_gaq.push(['second._setCustomVar', 2, 'customVar2', 'cv2', 1]);
It is working, but I have both custom vars in both accounts. What I really need, is to track customVar1 only for UA-XXXXXXX-1 account, and customVar2 only for UA-XXXXXXX-2 account. Any ideas how to implement this?
First of all, _setCustomVar must come before _trackPageview.
Now to your problem:
This happens because User level custom vars are stored in the cookie. Since both your trackers share the same cookie the second tracker will be sent with the vars set on the first tracker.
You have 3 options.
1) Go With Universal Analytics
The right path here is to use Universal Analytics. Multi-tracking is not officially supported in Classic because it's buggy, as you probably noticed. And things are easy to break.
On Universal all custom dimensions are evaluated server side so this setup is supported. No data is stored on cookies for Custom Dimensions.
eg:
Provided you configured dimension1 on UA-XXXXXXX-1 and dimension2 on UA-XXXXXXX-2 through the Admin interface.
ga('create', 'UA-XXXXXXX-1', 'auto');
ga('send', 'pageview', {
'dimension1': 'cv1'
});
ga('create', 'UA-XXXXXXX-2', 'auto', {'name': 'newTracker'});
ga('newTracker.send', 'pageview', {
'dimension2': 'cv2'
});
More info:
Universal Analytics Advanced Configuration / Multiple Tracking Objects
Universal Analytics Custom Dimensions and Metrics
2) Keep Classic Analytics but, use session level customVars
If you definitively can't move to Universal Analytics and want to keep using Classic you can get around this issue by just using Session Level Custom Vars. To make it work you would only need to change the scope of the custom Var as seen below (from 1 to 2).
Unlike User scoped Custom Vars, Session Scoped CVs don't get stored on the cookie. So you will get around this issue. The downside is that the value will only be valid for that session, not future sessions from the same user.
_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_gaq.push(['_setCustomVar', 1, 'customVar1', 'cv1', 2]);
_gaq.push(['_trackPageview']);
_gaq.push(['second._setAccount', 'UA-XXXXXXX-2']);
_gaq.push(['second._setCustomVar', 2, 'customVar2', 'cv2', 2]);
_gaq.push(['second._trackPageview']);
3) Keep Classic and User scoped CVs but use different cookies per tracker
You can configure GA to create 2 sets of cookies, one for each tracker one at the root domain and one at the subdomain.
If your site is: http://www.example.net setup your trackers like this:
_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_gaq.push(['_setDomainName', 'example.net']);
_gaq.push(['_setCustomVar', 1, 'customVar1', 'cv1', 1]);
_gaq.push(['_trackPageview']);
_gaq.push(['second._setAccount', 'UA-XXXXXXX-2']);
_gaq.push(['second._setDomainName', 'www.example.net']);
_gaq.push(['second._setCustomVar', 2, 'customVar2', 'cv2', 1]);
_gaq.push(['second._trackPageview']);
This MUST to be done in all pages of your site. Not only this one. This will make sure that each tracker uses it's isolated cookieset and customVars won't leak from one to another.
Notice that if your site can be accessed without www.. eg: http://example.net/ this will fail and there is no workaround. You can't create 2 sets of cookies with the same name in the same domain and path. You just can't.
Also if you use _gaq.push(['_setDomainName', 'none']); or _gaq.push(['_setAllowHash', false]);, the above trick won't work and cookies will conflict. Your data will be weird. Just don't do it. You've been warned.
I can't stress enough that this is provided without guarantees and if your data breaks it's on you. Multiple trackers are tricky and that's why it was never officially supported.
More info:
ga.js API Reference Domains and Directories