0

I have this in my template layout, running EE 3.4:

{if logged_in_group_id == "5"}
    <script>dataLayer = [{'userID': '{logged_in_username}'}];</script>
    Rest of the script pass data to Google Tag Manager
{/if}

It works most of the time – ie the usernames of the logged_in users are sent to Google Tag Manager. But sometimes it just sends the string "{logged_in_username}" instead, without parsing the username.

Does anyone have a clue why this is happening? This conditional is in a template layout and just after the <body> tag. If users are not logged in, they are redirected to another page.

2 Answers 2

1

When using EE variables inside of javascript, it's always best to separate the variables out onto separate lines to prevent the template parser from treating them as javascript objects (and thus skipping them).

{if logged_in_group_id == "5"}
    <script>dataLayer = [{
        'userID': '{logged_in_username}'
    }];</script>
    Rest of the script pass data to Google Tag Manager
{/if}
1
  • Pardon the late reply, I have been monitoring my GTM data for a few days before replying. The strange thing is, after I've separated the variables into separate lines, there have been no data passed at all. Would it make a difference to use {exp:member:custom_profile_data}{if group_id == "5"} instead?
    – Xiao Ling
    Oct 25, 2016 at 13:53
0

instead of using default variables, try some member plugin. Install https://devot-ee.com/add-ons/zeal-smart-members and use this code.

{if logged_in}
{exp:smart_members:profile}
    {if group_id == 5}
    <script>dataLayer = [{'userID': '{username}'}];</script>
    Rest of the script pass data to Google Tag Manager
    {/if}
{/exp:smart_members:profile}
{if:else}
    {redirect="__page_where_you_want_to_redirect__"}
{/if}
3
  • 1
    When promoting a product you are affiliated with as an answer, it's customary to mention it. Oct 19, 2016 at 13:41
  • Hi Mufaddal, thanks for the tip, I'm hesitant to use add-ons, unless absolutely necessary to. In this case, why would the plugin help when {username} is also a standard variable?
    – Xiao Ling
    Oct 25, 2016 at 13:56
  • You are right. I am also irritate to use addons. It happens with me sometime EE default variable not parse in some cases. But plugin parse it properly. That is the only reason I suggested to use plugin. Although I didn't tested above code so there is no guarantee of work. you must need to code and compile. Oct 25, 2016 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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