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.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
Fix issue with sendBeacon request.
Mike Cao17790aa5a891
diff --git a/.eslintrc.json b/.eslintrc.jsonindex 168665fc7..e5f0cc94f 100644--- a/.eslintrc.json+++ b/.eslintrc.json@@ -16,7 +16,8 @@"rules": {"react/display-name": "off","react/react-in-jsx-scope": "off",- "react/prop-types": "off"+ "react/prop-types": "off",+ "import/no-anonymous-default-export": "off"},"globals": {"React": "writable"diff --git a/lib/middleware.js b/lib/middleware.jsindex 228b22873..0c6fd081c 100644--- a/lib/middleware.js+++ b/lib/middleware.js@@ -3,11 +3,6 @@ import { getSession } from './session';import { getAuthToken } from './auth';import { unauthorized, badRequest, serverError } from './response';-const corsOptions = {- origin: '*',- credentials: true,-};-export function createMiddleware(middleware) {return (req, res) =>new Promise((resolve, reject) => {@@ -20,7 +15,7 @@ export function createMiddleware(middleware) {});}-export const useCors = createMiddleware(cors(corsOptions));+export const useCors = createMiddleware(cors());export const useSession = createMiddleware(async (req, res, next) => {let session;diff --git a/lib/request.js b/lib/request.jsindex 725dc3839..89e9c6559 100644--- a/lib/request.js+++ b/lib/request.js@@ -86,3 +86,11 @@ export async function getClientInfo(req, { screen }) {return { userAgent, browser, os, ip, country, device };}++export function getJsonBody(req) {+ if (req.headers['content-type'].indexOf('text/plain') !== -1) {+ return JSON.parse(req.body);+ }++ return req.body;+}diff --git a/lib/session.js b/lib/session.jsindex 167fda9ca..694465bc9 100644--- a/lib/session.js+++ b/lib/session.js@@ -1,9 +1,9 @@import { getWebsiteByUuid, getSessionByUuid, createSession } from 'lib/queries';-import { getClientInfo } from 'lib/request';+import { getJsonBody, getClientInfo } from 'lib/request';import { uuid, isValidUuid, parseToken } from 'lib/crypto';export async function getSession(req) {- const { payload } = req.body;+ const { payload } = getJsonBody(req);if (!payload) {throw new Error('Invalid request');diff --git a/pages/api/collect.js b/pages/api/collect.jsindex 2f0017585..a6ea8f1ee 100644--- a/pages/api/collect.js+++ b/pages/api/collect.js@@ -2,7 +2,7 @@ import isbot from 'isbot';import ipaddr from 'ipaddr.js';import { savePageView, saveEvent } from 'lib/queries';import { useCors, useSession } from 'lib/middleware';-import { getIpAddress } from 'lib/request';+import { getJsonBody, getIpAddress } from 'lib/request';import { ok, badRequest } from 'lib/response';import { createToken } from 'lib/crypto';import { removeTrailingSlash } from 'lib/url';@@ -39,10 +39,11 @@ export default async (req, res) => {await useSession(req, res);const {- body: { type, payload },session: { website_id, session_id },} = req;+ const { type, payload } = getJsonBody(req);+let { url, referrer, event_type, event_value } = payload;if (process.env.REMOVE_TRAILING_SLASH) {diff --git a/tracker/index.js b/tracker/index.jsindex e5277ed6d..37eea6310 100644--- a/tracker/index.js+++ b/tracker/index.js@@ -47,7 +47,7 @@ import { removeTrailingSlash } from '../lib/url';const post = (url, data, callback) => {const req = new XMLHttpRequest();req.open('POST', url, true);- req.setRequestHeader('Content-Type', 'application/json');+ req.setRequestHeader('Content-Type', 'text/plain');req.onreadystatechange = () => {if (req.readyState === 4) {@@ -114,20 +114,16 @@ import { removeTrailingSlash } from '../lib/url';const sendEvent = (value, type) => {const payload = getPayload();+payload.event_type = type;payload.event_value = value;- const blob = new Blob(- [- JSON.stringify({- type: 'event',- payload,- }),- ],- { type: 'application/json' },- );+ const data = JSON.stringify({+ type: 'event',+ payload,+ });- navigator.sendBeacon(`${root}/api/collect`, blob);+ navigator.sendBeacon(`${root}/api/collect`, data);};const addEvents = node => {