Send the user ID to GA4

Created on 23 October 2023, about 1 year ago
Updated 24 October 2023, about 1 year ago

Any pointers on how to add the add the user_id parameter to the config command in the measurement code on each page request?

GA4 info: https://developers.google.com/analytics/devguides/collection/ga4/user-id...

gtag('config', 'TAG_ID', {
  'user_id': 'USER_ID'
});

Thanks!

💬 Support request
Status

Fixed

Version

2.0

Component

Documentation

Created by

🇵🇹Portugal introfini

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @introfini
  • Assigned to introfini
  • Status changed to Fixed about 1 year ago
  • 🇵🇹Portugal introfini

    Here's a way:

    // Add the GA user_id to google_tag
    function bloom_custom_page_attachments_alter(array &$attachments)
    {
        $current_user = \Drupal::currentUser();
        if (isset($attachments["#attached"]["drupalSettings"]["gtag"]) && $current_user->isAuthenticated()) {
            $attachments["#attached"]["drupalSettings"]["gtag"]["additionalConfigInfo"]['user_id'] = 'drupal-user-id-' . $current_user->id();
        }
    }
    
    
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024