IsAdmin for MODX3

Hi There,

I’ve installed my first version of MODX3 and all great (nice work!) apart from one thing - I run a snippet on all my sites called: isAdmin, which detects if a manager is logged in or not - I use this to load Google Analytics only for logged out users so I don’t skew the numbers.
But ths snippet I’ve always used on all Modx 2 sites isn’t working on Modx 3.

This is the PHP:

<?php
if ($modx->user instanceof modUser) {
  if ($modx->user->hasSessionContext('mgr')) { 
    return true;
  }
}
return false;

Presumably the syntax needs updating for MODX 3 but I can’t find anything online - any suggestions?

Thanks!
Andy

You now have to add the namespace:

if ($modx->user instanceof MODX\Revolution\modUser) {

Boom! brilliant - thanks so much!!

1 Like

You could also just delete the test, since $modx->user is pretty much guaranteed to be an instance of the modUser or MODX\Revolution\modUser object.

Oh OK - so you mean like this?

So i’ve run into another problem with this in modx 3.
it seem that when i try and load the GA4 tag into the isAdmin snippet, it ignores it.

This is my code:


[[!isAdmin:notempty=`
    
<!-- Not tracking GA for Manager Users -->
`:default=`

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-54FN5RJ');</script>
<!-- End Google Tag Manager -->
`]]

If logged in - it shows the comment correctly of:

<!-- Not tracking GA for Manager Users -->

But when I check a browser or machine which isn’t logged into the manager, there is nothing, where the tag should be.

For testing I changed the Google Script to just another script tag, and it loads fine.
So for some reason it doesn’t like the Google Analytics code, and outputs nothing.

I tried putting the javascript in a chunk, but still the same.

Has anyone had this before?

I don’t understand which it outputs nothing - not even the comments which top and tail the script.

MODX 3.0.3 still has some parsing issues. (And for some reasons unknown to me, they haven’t released a new version in months.)

This pull request probably fixes the issue:

And maybe apply this pull request as well:

Yes, that’s exactly what I meant.

Thanks for comfirming!

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.