'How to detect if Prevent Cross-site tracking is active on Safari 13 with javascript

I would like to detect when Prevent Cross-site tracking is active in a browser using JavaScript.

My challenge is trying to find a method to detect this and show a warning popup to the user when this cross-site tracking is disabled by a users browser. This became essential after Safari 13 disabled it as default. I believe this might be helpful for people who are using cookies on iframes.

There are some ways such as

Check if third-party cookies are enabled

and

https://gist.github.com/iansltx/18caf551baaa60b79206

which are trying to bypass this security feature with using different methods and some of them are not valid anymore.

Basically what I try to achieve is from an iframe, try to detect Prevent Cross-site tracking is enabled on the browser using JavaScript.

I tried using this code:

var receiveMessage = function (evt) {
  if (evt.data === 'MM:3PCunsupported') {
    document.getElementById('result').innerHTML = 'not supported';
  } else if (evt.data === 'MM:3PCsupported') {
    document.getElementById('result').innerHTML = 'supported';
  }
};
window.addEventListener("message", receiveMessage, false);

However, this solution doesn't seem to work any more.



Solution 1:[1]

You can try using Google Analytics on your site and adding code to detect if it loads. If it doesn't load, then its likely that tracker blockers are active.

Example:

<script>
var active;
</script>
<script src="https://googletagmanager.com/…" onerror="active = 1;"></script>
<script>
if (active !== 1) {
alert('Prevent cross site tracking is disabled');
} else {
alert("Ok")
}
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leo