How to use is_user_logged_in() to send a dataLayer push

  • Unknown's avatar

    Hi everyone,

    I am desperately trying for 4 days in a row to solve an issue but since my coding understanding is limited, I am stuck.

    Basically what I want to do is read from wordpress’s is_user_logged_in() function, and then based on the value I get retured, peform a certain operation.

    The issue is, I am always getting value returned 1, even when I’m not logged in (Tried even a fresh browser). I’m so far using the following JS script:

    <script>
    if ('<?php echo is_user_logged_in(); ?>') {
    	window.dataLayer = window.dataLayer || [];
    		window.dataLayer.push({
    		'userLoggedIn' : '1'
    		});
    }
    
    else{
    		window.dataLayer = window.dataLayer || [];
    		window.dataLayer.push({
    		'userLoggedIn' : '0'
    		});
    }
    </script>

    Any clues what i’m doing wrong?
    Worth to mention: I’m adding this code to the elementor code blocks, which promissed to execute this code in the <head> (based on my selection).

  • Hi there,

    The best place to seek further help are the WordPress.org forums:

    https://wordpress.org/support/forums/

    The forum you have posted is for the WordPress.com hosting platform, which is different from the WordPress software that your site is using. You can learn more about these differences here:

    WordPress.com vs. WordPress.org

  • Unknown's avatar

    Ups, so sorry for that.
    Thank you for pointing me to the right place!

  • The topic ‘How to use is_user_logged_in() to send a dataLayer push’ is closed to new replies.