Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom header to all the http calls make by matomojs. #16076

Open
SARAVANA1501 opened this issue Jun 17, 2020 · 17 comments
Open

Add custom header to all the http calls make by matomojs. #16076

SARAVANA1501 opened this issue Jun 17, 2020 · 17 comments
Labels
Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc.

Comments

@SARAVANA1501
Copy link

SARAVANA1501 commented Jun 17, 2020

We are hosting Matomo behind gateway service, which is capable of exposing part of matomo endpoint to internet. We need to authenticate every request by certain custom header. The token is available in website cookie which we are trying to track. Is it possible to add that token or key in every request made by matomojs?

@tsteur
Copy link
Member

tsteur commented Jun 17, 2020

@SARAVANA1501 you mean in the JS tracking request (going eg to matomo.php endpoint?)

@SARAVANA1501
Copy link
Author

Yeah

@tsteur
Copy link
Member

tsteur commented Jun 17, 2020

That's not possible just yet unless you manage to intercept these any XmlHttpRequest or maybe using service workers. Alternatively, you could create a PR that adds this feature.

@tsteur tsteur added the Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc. label Jun 17, 2020
@SARAVANA1501
Copy link
Author

SARAVANA1501 commented Jun 17, 2020

If provide a some technical detail to begin, I am glad to start.

@tsteur
Copy link
Member

tsteur commented Jun 17, 2020

It's hard to give much technical detail there. Basically, you want to look at the js/piwik.js file.

Add some public method that can be called similar to how this.trackEvent = function (category, action, name, value, customData, callback) { is currently defined.

eg this.setCustomRequestHeader(name, value).

You'd store this in a say configCustomHeaders[name] = value.

These custom headers you would need apply to the method in sendPostRequestViaSendBeacon and sendXmlHttpRequest.

To have this working you would also need to force using POST requests by calling _paq.push(['setRequestMethod', 'POST']).

You would later configure the headers like _paq.push(['setCustomRequestHeader', 'name', 'value...']);

@Findus23 @mattab do we see any issues with allowing people to send custom headers? I suppose if there's an XSS on the site or ad blockers they could configure any random custom headers but I suppose they could also just send the XmlHttpRequest to the Matomo directly if they wanted in that case.

Wondering if this was maybe better done in a custom tracker plugin see https://developer.matomo.org/guides/enrich-js-tracker in which case I would give you different instructions @SARAVANA1501

@SARAVANA1501
Copy link
Author

@Findus23 @mattab If you don’t see any issues with implementation, I will contribute to this.

@mattab
Copy link
Member

mattab commented Jul 13, 2020

@SARAVANA1501 sounds good, feel free to create a PR and we will review it eventually 👍 Also check to include automated tests in the file: https://github.com/matomo-org/matomo/blob/4.x-dev/tests/javascript/index.php. Thanks for your consideration!

@SARAVANA1501
Copy link
Author

@tsteur What about _paq.push(['setCustomRequestHeader', 'name', 'value...']) implementation for scripts generated by tag manager?

@tsteur
Copy link
Member

tsteur commented Sep 30, 2020

@SARAVANA1501 that could work to have this in the JS tracker. Not sure though what you mean by "for scripts generated by tag manager"?

@SARAVANA1501
Copy link
Author

@tsteur While creating container in tag-manager, it is generating tracker js file. we have to configure page with container similar to below snippet

image

Is it possible to add similar "setCustomRequestHeader" function on these generated files?

@tsteur
Copy link
Member

tsteur commented Oct 1, 2020

If it's added to the Matomo JS tracker in https://github.com/matomo-org/matomo/blob/4.x-dev/js/piwik.js then it will automatically also be included in the tag manager since the tag manager embeds the JS tracking code automatically.

@joelcoxokc
Copy link

joelcoxokc commented Aug 9, 2023

Hello, This is actually a critical one for us.
We are using a BFF infrastructure where all requests from the client are proxied through our backed.
We need to send two headers along with the requests to matomo so that they can be consumed in our proxy.

We would really like to use _paq.push(['setRequestHeader', 'MyHeader', 'MyValue']);

@sgiehl
Copy link
Member

sgiehl commented Aug 9, 2023

@joelcoxokc This hasn't been implemented yet, and there are no current plans to do so.

Also implementing this might need some additional effort. By default Matomo tries to send it's requests using sendBeacon. That method afaik does not support providing custom headers. So we also would need to fall back using XMLHttpRequest instead.

@joelcoxokc
Copy link

@sgiehl I have been digging through the code to see if I could implement it for you.
So just so I understand...

afaik and XMLHttpRequest do not support setting headers?

I understand that you are requesting the matomo.js
and then sending individual requests per tracking event.

Is it possible to add custom headers to those individual tracking events?
Or is that also run through afaik?

@joelcoxokc
Copy link

I see this line, and am wondering if it's possible to tap into this add add an additional header

xhr.setRequestHeader('Content-Type', configRequestContentType);

@sgiehl
Copy link
Member

sgiehl commented Aug 11, 2023

The header might need to be set at that place. But there need to be methods added to allow providing the headers to be set. In addition as soon as a custom header is set, the sendbeacon method needs to be disabled, as it doesn't support additional headers.

@arapangabriel
Copy link

I see this line, and am wondering if it's possible to tap into this add add an additional header

xhr.setRequestHeader('Content-Type', configRequestContentType);

Have same problem
We are using cloudflare whitch provide some access tokens.
Will be greate to have a possabilities to add a custom headers for all matomo requests like
headers: {
"CF-Access-Client-Id": "token-id",
"CF-Access-Client-Secret":
"token-secret"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc.
Projects
None yet
Development

No branches or pull requests

7 participants