mixpanel / mixpanel-js

Official Mixpanel JavaScript Client Library
https://mixpanel.com/help/reference/javascript
Other
867 stars 309 forks source link

Can `api_host` contain a /path? #408

Open CoryDanielson opened 5 months ago

CoryDanielson commented 5 months ago

Can the api_host parameter be set to a directory instead of just a host? I skimmed the code and that appears to be the case, but I just wanted to confirm.

Would this work as expected

mixpanel.init("token-here", {
  api_host: "https://much-proxy.wow/m"
});

Would all requests within the client be sent to much-proxy.wow/m?

CoryDanielson commented 5 months ago

Looking through the code samples where api_host is used, and it seems like straight-forward string manipulation. Is there any other known reason why having a proxy with a path would not work?

https://github.com/search?q=repo%3Amixpanel%2Fmixpanel-js+path%3Asrc%2F*+api_host&type=code

tdumitrescu commented 5 months ago

Your example (api_host: "https://much-proxy.wow/m") will work for now based on the current implementation, but you're essentially relying on an implementation detail. I'd recommend sticking to the api_routes config option if you want to change paths, since that's what it's for.

CoryDanielson commented 5 months ago

Your example (api_host: "https://much-proxy.wow/m") will work for now based on the current implementation, but you're essentially relying on an implementation detail. I'd recommend sticking to the api_routes config option if you want to change paths, since that's what it's for.

I actually meant to use the api_host as the base of the URL, and the api_routes to all branch off of that. I'm wondering/clarifying if api_host explicitly needs to be the host portion of the URL, or if it's just a slight misnomer.

mixpanel.init(`my token`, {
  api_host: `https://much-proxy.wow/m`,
  api_routes: {
    track: `foo`,    // https://much-proxy.wow/m/foo
    engage: `bar`,   // https://much-proxy.wow/m/bar
    groups: `baz`,   // https://much-proxy.wow/m/baz
  },
));

I ask because I am interested in setting up the api_proxy as a nested route in an existing service, rather than standing up an entirely new nginx server.


In a test locally, I see this api_host working as I expected/hoped Screenshot 2024-01-05 at 11 16 07 AM

tdumitrescu commented 5 months ago

Yes, I'm just saying that you shouldn't rely on that behavior, as it's an implementation detail and might change without warning in future versions. The safer config for you would be:

mixpanel.init(`my token`, {
  api_host: `https://much-proxy.wow`,
  api_routes: {
    track: `m/foo`,    // https://much-proxy.wow/m/foo
    engage: `m/bar`,   // https://much-proxy.wow/m/bar
    groups: `m/baz`,   // https://much-proxy.wow/m/baz
  },
));