plausible / plausible-tracker

Frontend library to interact with Plausible Analytics
https://github.com/plausible/plausible-tracker
MIT License
214 stars 46 forks source link

External ApiHost to support full path (api/event only makes sense for plausible.io) #29

Closed tonysaad closed 2 years ago

tonysaad commented 2 years ago

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch plausible-tracker@0.3.5 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/plausible-tracker/.DS_Store b/node_modules/plausible-tracker/.DS_Store
new file mode 100644
index 0000000..5172429
Binary files /dev/null and b/node_modules/plausible-tracker/.DS_Store differ
diff --git a/node_modules/plausible-tracker/src/lib/request.ts b/node_modules/plausible-tracker/src/lib/request.ts
index 8867bce..898c552 100644
--- a/node_modules/plausible-tracker/src/lib/request.ts
+++ b/node_modules/plausible-tracker/src/lib/request.ts
@@ -67,7 +67,7 @@ export function sendEvent(
   };

   const req = new XMLHttpRequest();
-  req.open('POST', `${data.apiHost}/api/event`, true);
+  req.open('POST', `${data.apiHost.includes('plausible.io') ? `${data.apiHost}/api/event` : data.apiHost}`, true);
   req.setRequestHeader('Content-Type', 'text/plain');
   req.send(JSON.stringify(payload));
   // eslint-disable-next-line functional/immutable-data

This issue body was partially generated by patch-package.

TheStachelfisch commented 2 years ago

Hi, consider creating a pull request for this if the code you posted works. This will speed up the merging of this.