Skip to content

Commit

Permalink
fix: add custom rewrite for analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
olexh committed Aug 1, 2024
1 parent 733ea0d commit e995f24
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
11 changes: 6 additions & 5 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import bundleAnalyzer from '@next/bundle-analyzer';
import { withPlausibleProxy as plausibleProxy } from 'next-plausible';
import Icons from 'unplugin-icons/webpack';

/** @type {import('next').NextConfig} */
Expand Down Expand Up @@ -38,6 +37,10 @@ const nextConfig = {
source: '/api/:path*',
destination: `${process.env.API_URL}/:path*`,
},
{
source: '/event',
destination: `https://analytics.hikka.io/api/event`,
},
];
},
async redirects() {
Expand All @@ -60,8 +63,6 @@ const withBundleAnalyzer = bundleAnalyzer({
enabled: process.env.ANALYZE === 'true',
});

const withPlausibleProxy = plausibleProxy({
customDomain: 'https://analytics.hikka.io',
});
// const withPlausibleProxy = plausibleProxy({});

export default withBundleAnalyzer(withPlausibleProxy(nextConfig));
export default withBundleAnalyzer(nextConfig);
2 changes: 1 addition & 1 deletion public/js/plausible.local.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var r = window.location,
o = window.document,
l = o.currentScript,
s = l.getAttribute('data-api') || 'https://api.hikka.io/event';
s = 'https://hikka.io/event';

function t(t, e) {
try {
Expand Down
66 changes: 65 additions & 1 deletion public/js/script.js
Original file line number Diff line number Diff line change
@@ -1 +1,65 @@
!function(){"use strict";var a=window.location,r=window.document,o=r.currentScript,l=o.getAttribute("data-api")||new URL(o.src).origin+"/api/event";function s(t,e){t&&console.warn("Ignoring Event: "+t),e&&e.callback&&e.callback()}function t(t,e){if(/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(a.hostname)||"file:"===a.protocol)return s("localhost",e);if(window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)return s(null,e);try{if("true"===window.localStorage.plausible_ignore)return s("localStorage flag",e)}catch(t){}var n={},i=(n.n=t,n.u=a.href,n.d=o.getAttribute("data-domain"),n.r=r.referrer||null,e&&e.meta&&(n.m=JSON.stringify(e.meta)),e&&e.props&&(n.p=e.props),new XMLHttpRequest);i.open("POST",l,!0),i.setRequestHeader("Content-Type","text/plain"),i.send(JSON.stringify(n)),i.onreadystatechange=function(){4===i.readyState&&e&&e.callback&&e.callback()}}var e=window.plausible&&window.plausible.q||[];window.plausible=t;for(var n,i=0;i<e.length;i++)t.apply(this,e[i]);function p(){n!==a.pathname&&(n=a.pathname,t("pageview"))}var c,w=window.history;w.pushState&&(c=w.pushState,w.pushState=function(){c.apply(this,arguments),p()},window.addEventListener("popstate",p)),"prerender"===r.visibilityState?r.addEventListener("visibilitychange",function(){n||"visible"!==r.visibilityState||p()}):p()}();
!(function () {
'use strict';
var a = window.location,
r = window.document,
o = r.currentScript,
l = o.getAttribute('data-api') || new URL(o.src).origin + '/event';
function s(t, e) {
t && console.warn('Ignoring Event: ' + t),
e && e.callback && e.callback();
}
function t(t, e) {
if (
/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(
a.hostname,
) ||
'file:' === a.protocol
)
return s('localhost', e);
if (
window._phantom ||
window.__nightmare ||
window.navigator.webdriver ||
window.Cypress
)
return s(null, e);
try {
if ('true' === window.localStorage.plausible_ignore)
return s('localStorage flag', e);
} catch (t) {}
var n = {},
i =
((n.n = t),
(n.u = a.href),
(n.d = o.getAttribute('data-domain')),
(n.r = r.referrer || null),
e && e.meta && (n.m = JSON.stringify(e.meta)),
e && e.props && (n.p = e.props),
new XMLHttpRequest());
i.open('POST', l, !0),
i.setRequestHeader('Content-Type', 'text/plain'),
i.send(JSON.stringify(n)),
(i.onreadystatechange = function () {
4 === i.readyState && e && e.callback && e.callback();
});
}
var e = (window.plausible && window.plausible.q) || [];
window.plausible = t;
for (var n, i = 0; i < e.length; i++) t.apply(this, e[i]);
function p() {
n !== a.pathname && ((n = a.pathname), t('pageview'));
}
var c,
w = window.history;
w.pushState &&
((c = w.pushState),
(w.pushState = function () {
c.apply(this, arguments), p();
}),
window.addEventListener('popstate', p)),
'prerender' === r.visibilityState
? r.addEventListener('visibilitychange', function () {
n || 'visible' !== r.visibilityState || p();
})
: p();
})();
2 changes: 1 addition & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const RootLayout = ({ children }: Props) => {
trackLocalhost
enabled
selfHosted
customDomain="https://analytics.hikka.io"
customDomain="https://hikka.io"
domain="hikka.io"
/>
</head>
Expand Down

0 comments on commit e995f24

Please sign in to comment.