setting up user_id for Google Analytics - google-analytics

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).

Related

Cannot create reports for custom dimensions/metrics GA4

I have implemented a feedback star rating on my website at the bottom of each page. They can select one to five stars and optionally send free-form comments. I have implemented gtag.js to send the star rating integer and comments to my GA4 dashboard. Code snippet is below; I have omitted the JavaScript which allows the user to set the variables, but this is working.
<script async src="https://www.googletagmanager.com/gtag/js?id=MEASUREMENT_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'MEASUREMENT_ID', {
'custom_map': {
'rating_dimension': 'rating_key',
'comments_dimension': 'comments_key'
},
'rating_key': 'this.value',
'comments_key': 'commentsValue',
});
</script>
gtag('event', 'rating_event', {'rating_key': this.value });
gtag('event', 'comments_event', {'comments_key': commentValue });
I went under Configure>Custom definitions and added the dimensions under the Event scope.
The data is being sent as an event, because this is something the user can optionally do on a page. I have verified that the data is sent to GA, I have the Google Chrome GA Debugger, and can see that it is arriving in the Realtime report under Events.
I would like to build a report which shows the page title/URL, the number of views, the length of time spent, along with its average star rating and any comments. I would like to have some adjustable controls so that I can configure this to be shown for a specified length of time. For example, if the page is modified, I can see the average rating before and after the change. Unfortunately, I am not able to build even the most rudimentary view of this data in the new GA web interface. I tried to modify various existing reports to include my custom dimensions, but they don't offer this option. I went under Explore and tried to add a Freeform report, but I can't get the data to show up there either, it only counts the number of times the event has occurred, which is not what I care about at all.
I have gone to a lot of effort to send the data to GA and had expected to be able to access the data and build a basic report.
Is it that you are unable to add custom dimensions and metrics to the free form exploration? Or when you want to add dimensions or metrics, they don't even show up in the custom section?
Normally you have to wait more or less (some say up to) 24 hours before being able to use custom definitions (metrics and dimensions) in any report.
this might not be your case, but I found your post looking for an answer to my problem, which is the custom metrics and dimensions are there, I am just unable to add them to the report, it looks like a bug, I click the + button, check the custom dimensions and nothing happens.

Google Analytics UserID API extraction

Trying to extract a userID from GoogleAnalytics. This is to view which user is the using the website most and least.
I would to retrieve the top 5 user IDs and bottom 5 user IDs that were passed using either:
gtag('config', 'GA_TRACKING_ID', {
'user_id': 'USER_ID'
});
OR
using a custom dimension... ( https://support.google.com/analytics/answer/2709828?hl=en )
I'm (vaguely) aware of policies and TOS to keep 'non identifying' information on Google BUT there are posts online indicating you can link back to CMS data.
Steps so far
Google Analytics with UserID and view setup - Working in Google dashboard and showing filtered userID and All website data using the idea.
Requirements:
Extract page view and session data for each userId between a date
range (or all by default)
UserID via standard GA method
UserID via Custom dimension method
Any help, pointers or examples how someone has completed something like this are appreciated.
NOTE: This is to PULL data out of GA and manipulate/display it on an external system/dashboard.
Seen this which states it's not possible: Google analytics userID tracking
and this which states it (kind of) is google analytics API implementation for tracking a specific user activities in php
The solution I used:
Tracking
Create Google Analytics account
Create a new view by activating the UserID tracking (labeled NewView1)
Use https://developers.google.com/analytics/devguides/collection/gtagjs/custom-dims-mets
i.e. Define your custom dimension
Get Analytics tracking code + Add custom definition code
Create a Custom report using the 'metrics' you want to see and filtering by the 'custom dimension' I created earlier.
(note: data took ~ 12 hours to be visible so don't expect to work instantly)
Front end tracking additions
gtag('config', 'GA_TRACKING_ID', {
'custom_map': {'dimension<Index>': 'dimension_name'}
});
// Sends the custom dimension to Google Analytics.
gtag('event', 'any_event_name', {'dimension_name': dimension_value});
Extraction
Create New Google Developer Console Project (API)
Use a Service Account to connect the API with Analytics ( https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php ) -- API -> credentials -> Create Credentials (Service Account key)
(assign role [mine was set to Project => View])
Save the Text file to your filesystem and rename to json as per examples
Add permissions to your Analytics project by copy/pasting the 'Email' from the Service account details into Analytics User Management.
Get the view ID of the data you wish to extract (Analtyics created in step 2 in tracking)
Use the sample Code (HelloAnalytics.php) to connect and extract data
Use your custom dimension to filter results
The dimension filter I used was $dimensions (see below)
...
$dimensions = new \Google_Service_AnalyticsReporting_Dimension();
$dimensions->setName('ga:dimension1'); // as per docs
// Create the ReportRequest object.
$request = new \Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($sessions, $pageviews));
$request->setDimensions($dimensions);
From there I was able to see the same data via API that I could see in the custom report on analytics web.
NOTE: be careful of which Google project and view you're in while setting up
permissions and dimensions.
NOTE: using gtag() code and not Universal (ga()) js code
The answer is a very brief/rough summary of how I achieved my specific goal. It is not a one-size-fits all solution but hopefully it will give someone a better idea of how to set and extract custom variable data within Google.
The final result was data from the API. From there it's up to you.

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.

Google analytics api Get the userID / ClientID

I want to get the usage data of my customer out of google analytics, to make some usability analysis with it. For this, I need something that I can determine, which event was done by which user. Is there a possibility in a google analytics api to get this two parts linked?
You could add a user ID as a custom dimension, you'd probably want to set the scope to user.
https://support.google.com/analytics/answer/2709828?hl=en
This would allow you to connect up which users performed which actions.
Of course you can! :) I have used it in a lot of projects.
https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits
Example:
ga('send', {
hitType: 'event',
eventCategory: 'Video',
eventAction: 'play',
eventLabel: 'cats.mp4'
});
Since clientId has been made available through the API as ga:clientId you can use this value.
I would recommend using a custom User Id though - setting you own generated User Id in the script using the Universal Analytics User ID feature and in your backend db.
Also add the id to any link you send to your clients via email/sms/etc. that lands on your homepage to follow up on marketing performance.
You would need to have some javascript that grab the id and set it on the pageview. (like Linker, but adjusted for user id)
Note to anyone using the Universal Analytics User ID feature The values returned in ga:clientId is actually the userId Even more interessting. (As of time of writing) GA fails if you request clientId from a User ID view. So you should use a non-User ID view to get the User ID. :)

What is the correct syntax to set a custom dimension in 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.

Resources