Hide Matomo code for $user->isAdmin()

Matomo is installed on my website. Unfortunately, my access from localhost is often counted.
I have deactivated the Matomo code in the test environment:

<?php if ($site->url() != 'http://localhost:8888'): ?>
<!-- Matomo code -->
<?php endif ?>

In addition, I would like to hide Matomo when I am logged into the panel as admin and test pages on the server. How do I add the query?

<?php if ($site->url() != 'http://localhost:8888' || 
          kirby()->user()?->role()->name() !== 'admin'): ?>
<!-- Matomo code -->
<?php endif ?>
1 Like

Thanks for your hint.

(A) works:

<?php if ($site->url() != 'http://localhost:8888'): ?>
<!-- Matomo code -->
<?php endif ?>

(B) works:

<?php if (kirby()->user()?->role()->name() !== 'admin'): ?>
<!-- Matomo code -->
<?php endif ?>

(A) + (B) does not work:

<?php if ($site->url() != 'http://localhost:8888' || 
          kirby()->user()?->role()->name() !== 'admin'): ?>
<!-- Matomo code -->
<?php endif ?>

(A) outputs the correct URL and (B) the user ‘admin’.
Nevertheless, the combination does not work and the code is output and not suppressed.

Use && instead of ||

&& = solution
Why does the query still work under MAMP AND on the live server even though the first comparison is a negation? Shouldn’t the Matomo code then be displayed locally?

The query means:
IF URL is not equal to localhost AND “&&” user is not admin = output CODE
Wouldn’t an OR “||” be more logical? :thinking:

… or do I have logical problems in my way of thinking? :sunglasses:

|| means, either of the conditions A, B must be true for the stuff inside the if statement to happen.

So if condition A is true (you are not on localhost), then it will not test condition B, but execute the code inside your condition.

If condition A is false (you are on localhost), then and only then will PHP check the second condition.

1 Like

If in doubt, always use simple examples:

$a = true;
$b = false;

if ($a || $b) {
    echo 'hello';
}

if ($a && $b) {
    echo 'hello';
}

And then play around with it.

1 Like

I didn’t know the difference in the query until now.
Thanks for the clarification.
Very clearly explained, thank you very much. :clap: