How to call is_user_logged_in() within JavaScript

Solution:

Echoing a FALSE in PHP doesn’t print anything to the screen. This is why your first example didn’t work and it produced invalid JS when the user wasn’t logged in.

logintemp= ;

A simple way to do this is to make sure you json_encode the server side data so the JS can access it. Since JSON is a subset of JS, it will properly escape any content and produce a valid expression that can be assigned to a variable.

var isLoggedIn = <?php echo json_encode(is_user_logged_in()) ?>;

if (isLoggedIn) {
  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({
    'userLoggedIn': '1'
  });
} 
   
else {
   window.dataLayer = window.dataLayer || [];
   window.dataLayer.push({
     'userLoggedIn': '0'
   });
}

And the above can be simplified to

var isLoggedIn = <?php echo json_encode(is_user_logged_in()) ?>;
window.dataLayer = window.dataLayer || [];      
window.dataLayer.push({
   userLoggedIn: isLoggedIn ? '1' : '0'
});