Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Updated test console. Refactored fetch for tracker.

Mike Caobc75b622b56f
diff --cc components/pages/TestConsole.js
index 8a2afa886,9f4d7ca24..e4fa5a52f
--- a/components/pages/TestConsole.js
+++ b/components/pages/TestConsole.js
@@@ -1,4 -1,4 +1,3 @@@
--import React, { useState } from 'react';
import classNames from 'classnames';
import Head from 'next/head';
import Link from 'next/link';
@@@ -9,36 -9,35 +8,37 @@@ import DropDown from 'components/common
import WebsiteChart from 'components/metrics/WebsiteChart';
import EventsChart from 'components/metrics/EventsChart';
import Button from 'components/common/Button';
--import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
--import Icon from 'components/common/Icon';
import useFetch from 'hooks/useFetch';
--import useUser from 'hooks/useUser';
--import ChevronDown from 'assets/chevron-down.svg';
import styles from './TestConsole.module.css';
export default function TestConsole() {
-- const { user } = useUser();
-- const [website, setWebsite] = useState();
-- const [show, setShow] = useState(true);
-- const { basePath } = useRouter();
const { data } = useFetch('/websites');
++ const router = useRouter();
++ const {
++ basePath,
++ query: { id },
++ } = router;
++ const websiteId = id?.[0];
-- if (!data || !user?.is_admin) {
++ if (!data) {
return null;
}
const options = data.map(({ name, website_id }) => ({ label: name, value: website_id }));
++ const website = data.find(({ website_id }) => website_id === +websiteId);
const selectedValue = options.find(({ value }) => value === website?.website_id)?.value;
++ console.log({ websiteId, data, options, website });
++
function handleSelect(value) {
-- setWebsite(data.find(({ website_id }) => website_id === value));
++ router.push(`/console/${value}`);
}
function handleClick() {
-- window.umami('event (default)');
++ window.umami('umami-default');
window.umami.trackView('/page-view', 'https://www.google.com');
- window.umami.trackEvent('event (custom)', null, 'custom-type');
- window.umami.trackEvent('event (custom)', { test: 'test-data' }, 'custom-data-type');
- window.umami.trackEvent('event (custom)', 'custom-type');
++ window.umami.trackEvent('track-event-no-data');
++ window.umami.trackEvent('track-event-with-data', { test: 'test-data', time: Date.now() });
}
return (
@@@ -62,54 -61,54 +62,47 @@@
onChange={handleSelect}
/>
</PageHeader>
-- {!selectedValue && <EmptyPlaceholder msg="I hope you know what you're doing here" />}
-- {selectedValue && (
++ {website && (
<>
-- <div>
-- <Icon
-- icon={<ChevronDown />}
-- className={classNames({ [styles.hidden]: !show })}
-- onClick={() => setShow(!show)}
-- />
-- </div>
-- {show && (
-- <div className={classNames(styles.test, 'row')}>
-- <div className="col-4">
-- <PageHeader>Page links</PageHeader>
-- <div>
-- <Link href={`?page=1`}>
-- <a>page one</a>
-- </Link>
-- </div>
-- <div>
-- <Link href={`?page=2`}>
-- <a>page two</a>
-- </Link>
-- </div>
-- <div>
-- <Link href={`https://www.google.com`}>
-- <a className="umami--click--external-link">external link</a>
-- </Link>
-- </div>
++ <div className={classNames(styles.test, 'row')}>
++ <div className="col-4">
++ <PageHeader>Page links</PageHeader>
++ <div>
++ <Link href={`/console/${websiteId}?page=1`}>
++ <a>page one</a>
++ </Link>
++ </div>
++ <div>
++ <Link href={`/console/${websiteId}?page=2`}>
++ <a>page two</a>
++ </Link>
</div>
-- <div className="col-4">
-- <PageHeader>CSS events</PageHeader>
-- <Button
-- id="primary-button"
-- className="umami--click--primary-button"
-- variant="action"
-- >
-- Send event
-- </Button>
++ <div>
++ <Link href={`https://www.google.com`}>
++ <a className="umami--click--external-link-direct">external link (direct)</a>
++ </Link>
</div>
-- <div className="col-4">
-- <PageHeader>Javascript events</PageHeader>
-- <Button id="manual-button" variant="action" onClick={handleClick}>
-- Run script
-- </Button>
++ <div>
++ <Link href={`https://www.google.com`}>
++ <a className="umami--click--external-link-tab" target="_blank">
++ external link (tab)
++ </a>
++ </Link>
</div>
</div>
-- )}
++ <div className="col-4">
++ <PageHeader>CSS events</PageHeader>
++ <Button id="primary-button" className="umami--click--button-click" variant="action">
++ Send event
++ </Button>
++ </div>
++ <div className="col-4">
++ <PageHeader>Javascript events</PageHeader>
++ <Button id="manual-button" variant="action" onClick={handleClick}>
++ Run script
++ </Button>
++ </div>
++ </div>
<div className="row">
<div className="col-12">
<WebsiteChart
diff --cc next.config.js
index 09e67582e,10624451b..0401516c4
--- a/next.config.js
+++ b/next.config.js
@@@ -35,6 -4,9 +35,7 @@@ if (process.env.FORCE_SSL)
module.exports = {
env: {
currentVersion: pkg.version,
- loginDisabled: process.env.DISABLE_LOGIN,
- updatesDisabled: process.env.DISABLE_UPDATES,
- telemetryDisabled: process.env.DISABLE_TELEMETRY,
++ isProduction: process.env.NODE_ENV === 'production',
},
basePath: process.env.BASE_PATH,
output: 'standalone',
diff --cc pages/console/[[...id]].js
index 35b88ce3f,35b88ce3f..9028303c9
--- a/pages/console/[[...id]].js
+++ b/pages/console/[[...id]].js
@@@ -2,11 -2,11 +2,13 @@@ import React from 'react'
import Layout from 'components/layout/Layout';
import TestConsole from 'components/pages/TestConsole';
import useRequireLogin from 'hooks/useRequireLogin';
++import useUser from 'hooks/useUser';
--export default function TestPage() {
++export default function ConsolePage({ enabled }) {
const { loading } = useRequireLogin();
++ const { user } = useUser();
-- if (loading) {
++ if (loading || !enabled || !user?.is_admin) {
return null;
}
@@@ -16,3 -16,3 +18,9 @@@
</Layout>
);
}
++
++export async function getServerSideProps() {
++ return {
++ props: { enabled: !!process.env.ENABLE_TEST_CONSOLE },
++ };
++}
diff --cc tracker/index.js
index f0584288e,6b5714996..17ee467de
--- a/tracker/index.js
+++ b/tracker/index.js
@@@ -5,23 -5,23 +5,25 @@@ import { removeTrailingSlash } from '..
const {
screen: { width, height },
navigator: { language },
-- location: { hostname, pathname, search },
++ location,
localStorage,
document,
history,
} = window;
--
-- const script = document.querySelector('script[data-website-id]');
--
-- if (!script) return;
--
-- const attr = script.getAttribute.bind(script);
-- const website = attr('data-website-id');
-- const hostUrl = attr('data-host-url');
-- const autoTrack = attr('data-auto-track') !== 'false';
-- const dnt = attr('data-do-not-track');
-- const cssEvents = attr('data-css-events') !== 'false';
-- const domain = attr('data-domains') || '';
++ const { hostname, pathname, search } = location;
++ const { currentScript } = document;
++
++ if (!currentScript) return;
++
++ const _data = 'data-';
++ const _false = 'false';
++ const attr = currentScript.getAttribute.bind(currentScript);
++ const website = attr(_data + 'website-id');
++ const hostUrl = attr(_data + 'host-url');
++ const autoTrack = attr(_data + 'auto-track') !== _false;
++ const dnt = attr(_data + 'do-not-track');
++ const cssEvents = attr(_data + 'css-events') !== _false;
++ const domain = attr(_data + 'domains') || '';
const domains = domain.split(',').map(n => n.trim());
const eventClass = /^umami--([a-z]+)--([\w]+[\w-]*)$/;
@@@ -34,7 -34,7 +36,8 @@@
const root = hostUrl
? removeTrailingSlash(hostUrl)
-- : script.src.split('/').slice(0, -1).join('/');
++ : currentScript.src.split('/').slice(0, -1).join('/');
++ const endpoint = `${root}/api/collect`;
const screen = `${width}x${height}`;
const listeners = {};
let currentUrl = `${pathname}${search}`;
@@@ -68,88 -53,86 +56,82 @@@
const assign = (a, b) => {
Object.keys(b).forEach(key => {
-- a[key] = b[key];
++ if (b[key] !== undefined) a[key] = b[key];
});
return a;
};
- const collect = (type, payload, persist = false) => {
+ const collect = (type, payload) => {
if (trackingDisabled()) return;
- const endpoint = `${root}/api/collect`;
- let headers = { 'Content-Type': 'application/json' };
- if (cache) headers['x-umami-cache'] = cache;
- let options = {
+
- post(
- `${root}/api/collect`,
- {
- type,
- payload,
- },
- res => (cache = res),
- );
++ return fetch(endpoint, {
+ method: 'POST',
- body: JSON.stringify({type, payload}),
- headers
- };
- if (!persist) {
- fetch(endpoint, options).then(res => res.text()).then(resText => { cache = resText });
- } else {
- options['keepalive'] = true;
- fetch(endpoint, options);
- }
++ body: JSON.stringify({ type, payload }),
++ headers: assign({ 'Content-Type': 'application/json' }, { ['x-umami-cache']: cache }),
++ })
++ .then(res => res.text())
++ .then(text => (cache = text));
};
-- const trackView = (url = currentUrl, referrer = currentRef, uuid = website) => {
++ const trackView = (url = currentUrl, referrer = currentRef, uuid = website) =>
collect(
'pageview',
assign(getPayload(), {
website: uuid,
url,
referrer,
- })
+ }),
);
-- };
- const trackEvent = (event_name = 'custom', event_data, url = currentUrl, uuid = website) => {
- const trackEvent = (event_value, event_type = 'custom', url = currentUrl, uuid = website) => {
++ const trackEvent = (event_name, event_data, url = currentUrl, uuid = website) =>
collect(
'event',
assign(getPayload(), {
website: uuid,
url,
- event_type,
- event_value,
- })
+ event_name,
+ event_data,
+ }),
);
-- };
/* Handle events */
- const sendEvent = name => {
- const payload = getPayload();
-
- payload.event_name = name;
-
- const data = JSON.stringify({
- type: 'event',
- payload,
- });
-
- fetch(`${root}/api/collect`, {
- method: 'POST',
- body: data,
- keepalive: true,
- });
- const sendEvent = (event_value, event_type) => {
- collect(
- 'event',
- assign(getPayload(), {
- event_type,
- event_value,
- }),
- true
- );
-- };
--
const addEvents = node => {
const elements = node.querySelectorAll(eventSelect);
Array.prototype.forEach.call(elements, addEvent);
};
const addEvent = element => {
-- (element.getAttribute('class') || '').split(' ').forEach(className => {
++ const get = element.getAttribute.bind(element);
++ (get('class') || '').split(' ').forEach(className => {
if (!eventClass.test(className)) return;
- const [, type, name] = className.split('--');
- const [, type, value] = className.split('--');
++ const [, event, name] = className.split('--');
+
const listener = listeners[className]
? listeners[className]
-- : (listeners[className] = () => {
-- if (element.tagName === 'A') {
- sendEvent(name);
- sendEvent(value, type);
++ : (listeners[className] = e => {
++ if (
++ event === 'click' &&
++ element.tagName === 'A' &&
++ !(
++ e.ctrlKey ||
++ e.shiftKey ||
++ e.metaKey ||
++ (e.button && e.button === 1) ||
++ get('target')
++ )
++ ) {
++ e.preventDefault();
++ trackEvent(name).then(() => {
++ location.href = get('href');
++ });
} else {
- trackEvent(value, type);
+ trackEvent(name);
}
});
-- element.addEventListener(type, listener, true);
++ element.addEventListener(event, listener, true);
});
};