tags:
- analytics
product: "[[Obsidian Publish]]"
permalink: how-to-proxy-analytics-scripts
description: Learn how to unblock PostHog and Plausible analytics scripts on your website, ensuring accurate data tracking and insights.
image: https://mattnorris.dev/img/notes/how-to-proxy-analytics-scripts.png
Analytics scripts can be blocked by a provider like Netlify or Obsidian. Use reverse proxies to circumvent that.
Create a reverse proxy with Netlify. The netlify.toml
proxy looked like this:
# Proxy analytics requests to PostHog.
[[redirects]]
from = "/ingest/*"
to = "https://app.posthog.com/:splat"
host = "app.posthog.com"
status = 200
force = true
The PostHog docs recommend using a subdomain. Choose a "random" name that is unassociated with web analytics or its providers, e.g., i
for "ingest".
And the Obsidian Publish publish.js
file looked like this:
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures getActiveMatchingSurveys getSurveys onSessionId".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
DOMAIN = "https://YOUR-SUBDOMAIN.YOUR-DOMAIN.COM"
posthog.init("<YOUR-PROJECT-API-KEY>", {
api_host: DOMAIN + "/ingest",
ui_host: "https://app.posthog.com",
});
This was derived from the standard PostHog web snippet. I had to modify the posthog.init
function to use the proxy. Otherwise, it would look like this:
posthog.init("<YOUR-PROJECT-API-KEY>", {
api_host: "https://app.posthog.com",
});
Because PostHog worked, I did not complete Plausible's reverse proxy. Here are some helpful links to do so in the future.
I followed this forum post to configure Plausible analytics for Obsidian Publish.
var DOMAIN = "YOUR-DOMAIN.COM";
var plausibleSnippet = document.createElement("script");
plausibleSnippet.defer = true;
plausibleSnippet.setAttribute("data-domain", DOMAIN);
plausibleSnippet.src = "https://plausible.io/js/script.js";
document.head.appendChild(plausibleSnippet);
It did not work. But here is what I would try next:
I also tried to add Plausible to Docusaurus using option 1.
It did not work. But here is what I would try next: