API Key Authentication


I currently have a Collector(Cloud Run) running behind an API Gateway on Google Cloud Platform. API Gateway provides authentication thorough API Key which has to be passed as a query parameter as part of the HTTPS request, else the requests are not passed on to the collector.

I am currently using an Android Tracker to track events.

Is there a way to send query parameters when tracking events?

Thank you!

Unfortunately no, nothing like that is supported at the moment. The trackers are built with the use case of sending data directly to the collector in mind, and you can’t customise the requests without forking them.

1 Like

Hi @Colm,

Will there be a future release where such features(passing headers/ query params) would be made available?

Not sure about a future release but if you fork the tracker as @Colm mentioned it’s a reasonably minor addition to support this (for headers, I wouldn’t use query params) by adding a custom header to the request builder here.

1 Like

I actually faced the same problem with our trackers and didn’t want to fork the code as suggested, rather I just added custom code in the GTM to override base XML requests class, here is an example:

var o = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(){
  var res = o.apply(this, arguments);
  var err = new Error();  
  if((arguments[1] || '').indexOf('domain') > 0)
    this.setRequestHeader('apikey', '{your API key}');
  return res;
1 Like

I’ve opened a feature request on Github for this, it should be simple enough but a nice addition that I’m happy to accept into the codebase. I’ll see if we can squeeze it into the upcoming 3.2.0 release.

1 Like

Thank you @mike and @evaldas , ill try out both the ways.

Thanks @PaulBoocock , I’m looking forward to the release! It will make my development easier!


I wanted to share what I’ve done as a workaround to pass header for android tracker.

I created an OkHttpClient object with an Application Interceptor that adds api key as header. Created the NetworkConfiguration object and passed this client to the same.
Initialized the tracker using this network config.
The http/https requests now have the header

Code Images:

  1. Interceptor Class
  2. Network Config