• Resolved kdelves

    (@kdelves)


    I am attempting to use the custom GA code section to add a ‘set’ for userId tracking on an internal site however code placed within the entry box is stripped prior to insertion. Is it possible to use variables in these sections as below? Or does this only allow hard coded values to be entered?

    ga('set', 'userId', <?php echo get_current_user_id(); ?>);

    When entering the above, the below text ends up in the script on inspection.
    ga('set', 'userId', );

    My work around has been to enter the below directly inside the ga-google-analytics.php file underneath the custom code section:

    if ($gap_custom_code) echo $gap_custom_code . "\n\t\t\t";
    echo "ga('set', 'userId', '" . get_current_user_id() . "');" . "\n\t\t\t"
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jeff Starr

    (@specialk)

    Yeah the plugin settings do not accept PHP script, text only. But I like the idea and will add a shortcode that you can use to display the current user ID. Thank you for your feedback, and let me know if I can provide any further infos, glad to help.

    Thread Starter kdelves

    (@kdelves)

    Hey Jeff, thanks for the confirmation and speedy reply! I suspected this was the case.

    To clarify, would a shortcode work in place of a PHP script/section today or would support for this need to be added in?

    I’ve used the below code on another site previously to provide the WP user ID under [userid] which may help others looking for similar functionality.

    // Output User ID
    function user_id( ) {
    
      $current_user = wp_get_current_user();
      return $current_user->ID;
    
    }
    add_shortcode( 'userid', 'user_id' );
    Plugin Author Jeff Starr

    (@specialk)

    That’s the right idea, but yeah currently the plugin settings do not support any shortcodes, so that also will be added in the next version.

    Thread Starter kdelves

    (@kdelves)

    Legend, thanks for clarifying. Love your work!

    Plugin Author Jeff Starr

    (@specialk)

    Thank you kdelves!

    Plugin Author Jeff Starr

    (@specialk)

    Just to follow up with this. In the next version of the plugin, I’ve added a shortcode %%userid%% that can be added to the “Custom GA Code” setting to display the current user ID. Thanks again for your feedback, kdelves.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Custom GA Code functions’ is closed to new replies.