Google Analytics UserID API extraction

一世执手 提交于 2020-02-02 19:33:49

问题


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:


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.



来源:https://stackoverflow.com/questions/53756051/google-analytics-userid-api-extraction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!