0

I'd like to submit a Google Analytics event when a user creates an account.

Right now I'm checking for the presence of a 'first_login' user meta value on every page load. If it doesn't exist, the event JS script is enqueued and then the 'first_login' meta key is set.

For example:

// Enqueue a script that triggers a "new user" Google Analytics event
function new_user_tracking_script(){

    // Check if first visit time is unset
    if ( is_user_logged_in() && !get_user_meta(get_current_user_id(), 'first_login', true) ){

        // Google Analytics
        wp_enqueue_script( 'new_user_tracking' );

        // Set first visit time
        update_user_meta( get_current_user_id(), 'first_login', time() );

    }

}

This works but feels gross. Is there a better way to handle something like this?

1

This is how you would use the user_register hook to load your script:

add_action( 'user_register', 'new_user_tracking_script' )

function new_user_tracking_script(){
    // Google Analytics
    wp_enqueue_script( 'new_user_tracking' );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.