Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/recorder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ import { record } from 'rrweb';
const { currentScript } = document;
if (!currentScript) return;

const globalConfig = window.umamiConfig || {};
const _data = 'data-';
const attr = currentScript.getAttribute.bind(currentScript);
const config = value => attr(`${_data}${value}`);
const toCamelCase = str => str.replace(/-([a-z])/g, g => g[1].toUpperCase());
const config = value => {
const camelKey = toCamelCase(value);
if (globalConfig[camelKey] != null) {
const v = globalConfig[camelKey];
return Array.isArray(v) ? v.join(',') : String(v);
}
return attr(`${_data}${value}`);
};

const website = config(`website-id`);
const hostUrl = config(`host-url`);
Expand Down
21 changes: 16 additions & 5 deletions src/tracker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
doNotTrack,
} = window;
const { currentScript, referrer } = document;
if (!currentScript) return;
const globalConfig = window.umamiConfig || {};
if (!currentScript && !globalConfig.websiteId) return;

const { hostname, href, origin } = location;

Expand All @@ -23,8 +24,18 @@
const _data = 'data-';
const _false = 'false';
const _true = 'true';
const attr = currentScript.getAttribute.bind(currentScript);
const config = value => attr(`${_data}${value}`);
const attr = currentScript ? currentScript.getAttribute.bind(currentScript) : () => null;

const toCamelCase = str => str.replace(/-([a-z])/g, g => g[1].toUpperCase());

const config = value => {
const camelKey = toCamelCase(value);
if (globalConfig[camelKey] != null) {
const v = globalConfig[camelKey];
return value === 'before-send' && typeof v === 'function' ? v : Array.isArray(v) ? v.join(',') : String(v);
}
return attr(`${_data}${value}`);
};
Comment thread
Yashh56 marked this conversation as resolved.
Comment thread
Yashh56 marked this conversation as resolved.

const website = config('website-id');
const hostUrl = config('host-url');
Expand All @@ -40,7 +51,7 @@

const domains = domain.split(',').map(n => n.trim());
const host =
hostUrl || '__COLLECT_API_HOST__' || currentScript.src.split('/').slice(0, -1).join('/');
hostUrl || '__COLLECT_API_HOST__' || (currentScript ? currentScript.src.split('/').slice(0, -1).join('/') : '');
const endpoint = `${host.replace(/\/$/, '')}__COLLECT_API_ENDPOINT__`;
const screen = `${width}x${height}`;
const eventRegex = /data-umami-event-([\w-_]+)/;
Expand Down Expand Up @@ -163,7 +174,7 @@
const send = async (payload, type = 'event') => {
if (trackingDisabled()) return;

const callback = window[beforeSend];
const callback = typeof beforeSend === 'function' ? beforeSend : window[beforeSend];

if (typeof callback === 'function') {
payload = await Promise.resolve(callback(type, payload));
Expand Down