WordPress.org

Ready to get started?Download WordPress

Forums

Google Analytics for WordPress
How can I add my own custom variable (3 posts)

  1. jetansey
    Member
    Posted 1 year 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;
    get_currentuserinfo();
    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">
    //![CDATA[
    var _gaq=_gaq || [];_gaq.push(['_setCustomVar',4,'username','Jim',2]);
    //]]
    </script>

    http://wordpress.org/extend/plugins/google-analytics-for-wordpress/

  2. eliddon
    Member
    Posted 1 year ago #

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

  3. eliddon
    Member
    Posted 1 year ago #

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

    add_filter('yoast-ga-custom-vars','add_ga_custom_vars',10,2);
    
    function add_ga_custom_vars($push, &$customvarslot) {
       $my_custom_val = "whaterver";
    $push[] = "'_setCustomVar',".$customvarslot.",'My_Custom_Variable','".$my_custom_val."',3";
       $customvarslot++;
       return $push;
    }

    This worked for me. Hope it helps someone else.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic