How to set custom tags in microsoft clarity?

I have a wordpress website and added microsoft clarity for session recordings, heatmap, etc. I want to send username of my logged in users to microsoft clarity. so that i can see the particular user’s session recording. I don’t know coding so I got the help of AI and asked it to give the code.

<?php
// Get the username of the current user if logged in
$current_user = wp_get_current_user();
$username = ($current_user->ID != 0) ? $current_user->user_login : ‘guest’; // If user is not logged in, set username to ‘guest’
?>
<script type=”text/javascript”>
// Set the username in Clarity
clarity(“set”, “username”, “<?php echo $username; ?>”);
</script>

And I placed it using code snippets plugin in sitewide header. It gave me an issue. When I close my browser and again open my browser to my website. It logged me out of my website. But before inserting this code, I was still logged in to my website. After disabling the code, it works normal. I told this issue to the AI but the codes that it gave me all had the same problem.

How to send my user’s username to clarity as custom tag without this log out issue. Please help me. Thank You.

1 Comment
  1. Maybe change this variable name to something else `$username =` like `$usr = `, maybe `$username` is used in other parts too.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer