0
votes

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

1
I am a bit unclear what your are asking (that you don't already know) - you cannot query the userId data field, because the API does not make it available, but you can store the same value you use as the UserID to a custom dimension, at which point it will work like any other dimension (e.g. for use in a filter in your API query if you want a specific user). Can you elaborate again which parts gives you trouble ? - Eike Pierstorff
As for the Google TOS, they say that Google must not be able to match the value to a person (so if you match it externally you are within the TOS, although your legislation still might specify tighter rules ). If you use PII like e-mail addresses, Google requires that you hash it with SHA-256 or better. - Eike Pierstorff
Thank you Eike, my question was more of a confirmation so that I don't go too far down a path that leads nowhere. Being a first time Google API user your comments helped a lot. I will now investigate how to extract the required information from Google using a custom dimension value. Thank you. - JI-Web
FYI for those that are following: this is where I go to next stackoverflow.com/questions/20228251/… - JI-Web
UPDATE: using developers.google.com/analytics/devguides/reporting/core/v4/… -- A couple of steps required to setup API + connect with Analytics. + Also note that Custom Reports in Google Analytics get and present the data. Now just to replicate that via API... - JI-Web

1 Answers

0
votes

The solution I used:

Tracking

  1. Create Google Analytics account
  2. Create a new view by activating the UserID tracking (labeled NewView1)
  3. Use https://developers.google.com/analytics/devguides/collection/gtagjs/custom-dims-mets i.e. Define your custom dimension
  4. Get Analytics tracking code + Add custom definition code
  5. 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

  1. Create New Google Developer Console Project (API)
  2. 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)
  3. (assign role [mine was set to Project => View])
  4. Save the Text file to your filesystem and rename to json as per examples
  5. Add permissions to your Analytics project by copy/pasting the 'Email' from the Service account details into Analytics User Management.
  6. Get the view ID of the data you wish to extract (Analtyics created in step 2 in tracking)
  7. Use the sample Code (HelloAnalytics.php) to connect and extract data
  8. 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.

enter image description here