Skip to content

Instantly share code, notes, and snippets.

@alexminza
Last active December 28, 2023 02:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexminza/c5b40a009b88a5100785e3b2aa981dd2 to your computer and use it in GitHub Desktop.
Save alexminza/c5b40a009b88a5100785e3b2aa981dd2 to your computer and use it in GitHub Desktop.
WooCommerce Google Analytics Integration - add UserID tracking
<?php
//https://github.com/woocommerce/woocommerce-google-analytics-integration/blob/master/includes/class-wc-google-analytics-js.php
function woocommerce_ga_snippet_output_userid($code) {
$user_id = get_current_user_id();
if($user_id > 0) {
$ga_user_id = WC_Google_Analytics_JS::tracker_var() . "('set', 'userId', '" . $user_id . "');";
$code = $code . $ga_user_id;
}
return $code;
}
add_filter('woocommerce_ga_snippet_output', 'woocommerce_ga_snippet_output_userid');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment