Allow alternate host url in tracker.

This commit is contained in:
Mike Cao 2020-08-31 20:25:31 -07:00
parent aec012fb79
commit cebd3fd924
3 changed files with 9 additions and 5 deletions

View File

@ -1,11 +1,11 @@
export function removeTrailingSlash(url) {
return url.length > 1 && url.endsWith('/') ? url.slice(0, -1) : url;
return url && url.length > 1 && url.endsWith('/') ? url.slice(0, -1) : url;
}
export function getDomainName(str) {
try {
return new URL(str).hostname;
} catch {
} catch (e) {
return str;
}
}

View File

@ -1,6 +1,6 @@
{
"name": "umami",
"version": "0.19.0",
"version": "0.20.0",
"description": "A simple, fast, website analytics alternative to Google Analytics. ",
"author": "Mike Cao <mike@mikecao.com>",
"license": "MIT",

View File

@ -1,6 +1,7 @@
import 'promise-polyfill/src/polyfill';
import 'unfetch/polyfill';
import { post, hook, doNotTrack } from '../lib/web';
import { removeTrailingSlash } from '../lib/url';
(window => {
const {
@ -17,7 +18,10 @@ import { post, hook, doNotTrack } from '../lib/web';
if (!script || (__DNT__ && doNotTrack())) return;
const website = script.getAttribute('data-website-id');
const hostUrl = new URL(script.src).href.split('/').slice(0, -1).join('/');
const hostUrl = script.getAttribute('data-host-url');
const root = hostUrl
? removeTrailingSlash(hostUrl)
: new URL(script.src).href.split('/').slice(0, -1).join('/');
const screen = `${width}x${height}`;
const listeners = [];
@ -42,7 +46,7 @@ import { post, hook, doNotTrack } from '../lib/web';
});
}
return post(`${hostUrl}/api/collect`, {
return post(`${root}/api/collect`, {
type,
payload,
});