clarity icon indicating copy to clipboard operation
clarity copied to clipboard

[Feature Request] Adding option to add Reverse Proxy.

Open cyfyifanchen opened this issue 10 months ago • 12 comments

Adding option to add Reverse Proxy.

cyfyifanchen avatar Mar 29 '24 09:03 cyfyifanchen

yesssss pleeaaase...... Half of our traffic doesnt get recorded which is super frustrating...

kkopp001 avatar Mar 29 '24 21:03 kkopp001

I took a stab at this in our codebase.. I can re-route the /collect calls with something like this, but I can't seem to intercept the https://www.clarity.ms/tag call which is required to start the /collect calls. Abandoning for now. Just mentioning it to show interest in this ticket.

(function() {
    var proxiedXhr = window.XMLHttpRequest;

    window.XMLHttpRequest = function() {
        var xhr = new proxiedXhr();

        xhr.addEventListener("load", function(p) {
            const originalUrl = p.target.responseURL;
            console.log("Intercepted XMLHttpRequest call", originalUrl);
            if (originalUrl.startsWith("https://r.clarity.ms/collect")
             xhr.open('GET', "other-url/clarity-collect");
        });

        return xhr;
    };
})();

onyoo avatar Apr 26 '24 20:04 onyoo

An alternative way is to use the npm package clarity-js. By setting the upload parameter of the clarity configuration to a reverse proxy URL you can funnel your traffic through your domain.

Conversely, if you were to do a custom inline tag of https://www.clarity.ms/tag/<project_id> and modify the upload parameter before loading it into a script tag that could also be a viable option, at least until it's officially supported by Clarity.

NPM Configuration Approach: https://github.com/microsoft/clarity/issues/408#issuecomment-2095062739

z0nekill avatar May 06 '24 01:05 z0nekill

I thought the npm package clarity-js isn't recommended and the script tag is. But the workaround is nice, have you tested it?

cyfyifanchen avatar May 06 '24 22:05 cyfyifanchen

I'm using the npm package approach myself and have hardcoded the necessary values (works for now), I did take a look at using Regex to extract the configuration JSON from https://www.clarity.ms/tag/<project_id> to use with the NPM module but it was hacky - and prone to breaking.

However, your use case of modifying the upload parameter would be less hacky ;) as you would just be replacing

upload: "https://z.clarity.ms/collect" => "https://<mydomain>/collect"

But I wouldn't recommend it - because the script coming back from that service is bound to change, and so could the parameters and version of clarity-js returned as well.

z0nekill avatar May 07 '24 02:05 z0nekill

Good to know, thanks for the detailed info. Let me try it out and see what I can share here.

cyfyifanchen avatar May 08 '24 02:05 cyfyifanchen

It seems Mautic and Piwik are our only options because most of the visitors have adblockers.

TurkerTunali avatar May 22 '24 14:05 TurkerTunali

I tried proxying and it worked but it seems the collect endpoint takes the IP address of the proxy as the 'location' for the user session instead

TonsiTT avatar Jun 06 '24 09:06 TonsiTT

Have you tried setting X-Forwarded-For headers in your proxy setup?

z0nekill avatar Jun 06 '24 09:06 z0nekill

Have you tried setting X-Forwarded-For headers in your proxy setup?

Yup, didn't work

TonsiTT avatar Jun 07 '24 14:06 TonsiTT

I tried proxying and it worked but it seems the collect endpoint takes the IP address of the proxy as the 'location' for the user session instead

How did you proxy it? You and I seem to describe opposite problems (see my code above)

onyoo avatar Jun 10 '24 09:06 onyoo

I tried proxying and it worked but it seems the collect endpoint takes the IP address of the proxy as the 'location' for the user session instead

How did you proxy it? You and I seem to describe opposite problems (see my code above)

You can rewrite all clarity URLs to another domain name and just forward the call to the original URL. Alas, there is no use since it will give the wrong location on the clarity side

TonsiTT avatar Jun 11 '24 17:06 TonsiTT