diff --git a/lib/url.js b/lib/url.js index e29243fb..0eb4a04a 100644 --- a/lib/url.js +++ b/lib/url.js @@ -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; } } diff --git a/package.json b/package.json index 80bf5f9c..a6cca6bb 100644 --- a/package.json +++ b/package.json @@ -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 ", "license": "MIT", diff --git a/tracker/index.js b/tracker/index.js index a30a0220..f0ad78cd 100644 --- a/tracker/index.js +++ b/tracker/index.js @@ -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, });