Google Analytics 4: how to get the name of global object (gtag) - google-analytics

In Google Analytics Classic/Universal Analytics we could get the name of global object using such code:
window['GoogleAnalyticsObject']
And then we could get the object by its name:
window[window['GoogleAnalyticsObject']]
Looks like in GA4 such approach is not working, I am getting undefined if I try to get this name. Is it possible somehow to get this name in GA4 ?
Also this is my use case: I have a site on which GA4 was added, there is a possibility that the global object - gtag was renamed (but I don't know about that), theafore I need to get current name of the object and then to get the object by its name.

Per the updated Google Analytics 4 site tag documentation, it appears that object may now be in window.dataLayer. I'm thinking either:
window[window.dataLayer['GoogleAnalyticsObject']]
or
window.dataLayer['GoogleAnalyticsObject']

Related

Get Source and Medium from ga.js

I have a newsletter page on my website which I'd like to also use to capture some additional information about the user using Google Analytics.
Using the getAll() method from ga.js I can get the referrer URL (if any), however I cannot see the medium or source data. Is this possible to retrieve using their ga.js library?
Retrieve specific tracker from the list of trackers you obtained.
Use the following to access the fields source and medium.
tracker.get('campaignSource') and tracker.get('campaignMedium')
Additional help from below links.
Access data from ga object:
https://developers.google.com/analytics/devguides/collection/analyticsjs/accessing-trackers
Field reference:
https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference
This is not possible with ga.js.
However, you can use custom JavaScript to deduce the same information.
If you only want this capability, then I suggest you have a look at the library I wrote: https://github.com/ilkkapeltola/visitdata
Once the library is included, you can run visitData.get(), which returns an object with exactly the data you need.
If you are using google analytics core reporting api try this
'metrics': 'ga:sessions ,ga:pageviews',
'dimensions' : 'ga:source,ga:medium'

How to Track Store Locator Search terms in Google Universal Analytics

I am in the process of setting up enhanced Ecommerce for GA and have had a few requests of additional of what else the client would like to see in their reports. Once of which is
"Where can i see a report if the locations that a use types in when they use the store locator"
There is already an internal search functionality thats been set up and is tracking the search terms people are typing when looking for products, but i'm not sure if i would need to set this up as a second search terms report or if it's something different?
The URL of the page is different to the internal search results and is www.domain.com/store-locator#wales|GB|0|0|0
Any insight into this would be really helpful.
Thanks,
Roxi
As to me knowledge, you are not able to use bookmarks (#something) in GA for internal site search setup. Only GET and POST parameters are allowed. In your situation I think the best solution is to use GA events to send the data about used location each time the user is using this functionality. You need to include addition ga() function call to track those events. Info about how to set it up you could find here: event tracking. After setting things up, you will see all the info about number and type of called events in Behavior -> Events reports section in GA.
Example code:
<button onlick="var hash_location=window.location.hash;ga('send','event','Locator',has_location)">Click me</button>
With such function new event will be send to GA with Event Category=Locator and Event Action=hash in the url. You have some complicated hash, so most probably you need to extract first some info from this using regular expression. Example to get first item from |-separated list in the hash:
var pattern = new RegExp('[^#|]+');
var hash_location = pattern.exec(window.location.hash)[0];

Custom Dimension tracked in GTM, but not showing in UA

I tried to setup Google Tag Manager. The Pageviews are tracked correctly, but in Universal Analytics I can not see Custom Dimensions.
In Google Universal Analytics:
I have defined some custom dimensions in the Universal Analytics Profile under "Admin" -> "Custom Definitions" -> "Custom Dimensions".
The following Dimensions are relevant:
(Name) (Index)
PageView Author 7
PageView Themenwelt 8
PageView Seitentyp 9
PageView Posttyp 10
PageView Modified Date 11
In Google Tag Manager:
I have added a page Universal Analytics Tag with Track Type "Page View" and the following settings for "Custom Dimensions":
(Index) (Dimension)
7 {{Tracking Variable Posttyp}}
9 {{Tracking Variable Seitentyp}}
8 {{Tracking Variable Themenwelt}}
7 {{Tracking Variable Autor}}
11 {{Tracking Variable Modified Date}}
The values for the dimensions are fetched via Macros, using the "Javascript Variable" Macro.
Example for such a macro is just:
Global Variable Name: window.analytics_tracking_params.Author
The Variable is defined in the Website content, before the tag manager code.
When I go to the debug mode of tag manager, I can see the event being triggered, with the following debug entry for "Dimension":
{
7: Tracking Variable Autor,
8: Tracking Variable Themenwelt,
9: Tracking Variable Seitentyp,
10: Tracking Variable Posttyp,
11: Tracking Variable Modified Date,
}
The Macros resolve correctly to the values set on the website.
----------> I believe that the Google Tag Manager Setup is correct.
Again, in Google Universal Analytics:
I have created a Custom Report ("Customization" -> "+New Custom Report") with the following settings:
Metric Groups: Pageviews (Users)
Dimension Drilldowns: PageView Posttyp (Custom Dimensions)
In the report table below the Chart, there is only this message "There is no data for this view.".
This happens regardless of which Custom Dimension I chose.
----------> I believe that the Settings in Google Universal Analytics are correct, but apparently something does not work as expected.
It seems the Data is not correctly sent to Universal Analytics OR Universal Analytics is not able to process the Data and display it. Is there anything I might have forgotten or misunderstood?
I encountered a similar problem with my GTM configuration. I had my macro retrieving the values for my custom dimensions set up to fill in an empty string as a default value (see checkbox for default value: "To set the default value to be an empty string, select this option and leave the text field blank.").
Unfortunately my custom dimension was overwritten at some places with the empty string, so in GA pagehits etc. were recorded but the custom dimensions have been set to blank.
I changed that with unchecking the default value box, so instead of a empty string a nil value (or in JS: undefined) will be used instead. With that setting my custom dimension haven't got overwritten again with empty strings.
Maybe that might fix your problem as well?
see if your custom dimensions can be seen as aseconday dimension in the events sections. If you can you are probably look at standard user reports. Custom reports easily populate custom dimensions.

Accessing ClientID set by Google Analytics through Google Tag Manager

I have setup a new Universal Analytics tracking and would like to store ClientID in one of the custom dimensions slots.
The tracking tags are implemented through Google Tag Manager (using the predefined UA tag) and I am having the difficulty to passing the Client ID (via custom JavaScript Macro) with the basic pageview request.
I have named my tracker so that I can access it and this code runs without any error in console, however when used with GTM, it's returning an error.
ga.getByName('trackerGTM').get('clientId'))
produces an error:
Uncaught ReferenceError: ga is not defined
Any tips?
I assume you're firing the pageview {{url}} matches RegEx .*, correct? Could be that Universal hasn't loaded yet. Have you tried {{event}} equals gtm.dom? I haven't tested this, but it's a thought.
EDIT:
This looks like a bug. I was able to run:
function(){
var client = ga.getAll()[0].get('clientId');
return client;
}
And have it spit out the client ID with no problem.
The real problem is that GTM (Google Tag Manager) creates an object that is NOT called ga (it creates an object named ga followed by a random number), that's why 'ga' is not defined. You should try something on the lines of using the tag manager to put that client id into a javascript variable so you can access it from the page)
I was reading up on setting client ids and came across this blog link about accessing client ID in GA while using Google Tag Manager. Might be helpful.
Accessing Client ID in GA using custom dimensions

How to modify page URL in Google Analytics

How can you modify the URL for the current page that gets passed to Google Analytics?
(I need to strip the extensions from certain pages because for different cases a page can be requested with or without it and GA sees this as two different pages.)
For example, if the page URL is http://mysite/cake/ilikecake.html, how can I pass to google analytics http://mysite/cake/ilikecake instead?
I can strip the extension fine, I just can't figure out how to pass the URL I want to Google Analytics. I've tried this, but the stats in the Google Analytics console don't show any page views:
pageTracker._trackPageview('cake/ilikecake');
Thanks,
Mike
You could edit the GA profile and add custom filters ...
Create a 'Search and Replace' custom filter, setting the filter field to 'Request URI' and using something like:
Search String: (.*ilikecake\.)html$
Replace String: $1
(was \1)
Two possibilities come to mind:
it can take a while, up to about 24 hours, for visits to be reflected in the Analytics statistics. How long ago did you make your change?
try beginning the pathname with a "/", so
pageTracker._trackPageview('/cake/ilikecake');
and then wait a bit, as per the first item.
Usually you have the ga script code at the end of your file, while special _trackPageviews() calls are often used somewhere else.
Have you made sure you have your call to pageTracker._trackPageview() after you have defined the pagetracker?
Like this:
var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
pageTracker._trackPageview();
otherwise you just get a JavaScript error I suppose.

Resources