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.
[ad_2]
Maybe change this variable name to something else `$username =` like `$usr = `, maybe `$username` is used in other parts too.