Skip to content

Instantly share code, notes, and snippets.

@chavesm
Created April 27, 2021 01:11
Show Gist options
  • Save chavesm/8b86c58e66b3d1ca21bc3b4fd6412bd6 to your computer and use it in GitHub Desktop.
Save chavesm/8b86c58e66b3d1ca21bc3b4fd6412bd6 to your computer and use it in GitHub Desktop.
Send user agent as a custom dimension via MonsterInsights action hook
<?php // Remove this line when adding to child functions.php
/**
* This example sets custom dimension #14 to be the HTTP
* user agent.
*
* This is compatible with MonsterInsights gtag code. This filter supercedes
* the monsterinsights_frontend_tracking_options_analytics_before_pageview
* filter.
*
* Change the number 14 to your custom dimension index number in GA.
*/
add_filter(
monsterinsights_frontend_tracking_options_persistent_gtag_before_pageview,
function( $options ) {
$options[ 'dimension14'] = $_SERVER['HTTP_USER_AGENT'];
return $options;
},
20 // priority
);
@chavesm
Copy link
Author

chavesm commented Apr 27, 2021

GA doc for creating a custom dimension in a UA property: https://support.google.com/analytics/answer/2709829

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment