Google Analytics by MonsterInsights
[Plugin: Google Analytics for WordPress] How can I add my own custom variable (3 posts)

  1. jetansey
    Posted 4 years ago #

    How can I add my own custom variable? I want to track who logged in $current_user. I added this code to functions.php and it shows up in the <head> but below the yoast script?

    function jt_gatrackuser() {
    global $current_user;
    echo "\n";
    echo '<script type="text/javascript">' . "\n";
    echo '//![CDATA[' . "\n";
    echo 'var _gaq=_gaq || [];';
    echo '_gaq.push([\'_setCustomVar\',4,\'username\',\'';
    echo $current_user->user_login;
    echo '\',2]);' . "\n";
    echo '//]]' . "\n" . '</script>';
    echo "\n";
    add_action('wp_head', 'jt_gatrackuser');


    This is what get generated below the Yoast script
    <script type="text/javascript">
    var _gaq=_gaq || [];_gaq.push(['_setCustomVar',4,'username','Jim',2]);


  2. eliddon
    Posted 3 years ago #

    Did you ever find a solution for this? I want to do the same thing...

  3. eliddon
    Posted 3 years ago #

    I figured it out... You can use something like the code below:

    function add_ga_custom_vars($push, &$customvarslot) {
       $my_custom_val = "whaterver";
    $push[] = "'_setCustomVar',".$customvarslot.",'My_Custom_Variable','".$my_custom_val."',3";
       return $push;

    This worked for me. Hope it helps someone else.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Google Analytics by MonsterInsights
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic