posthog-analytics
Skill9 starsChanged 41 days agoMITView on GitHub
Set up, backfill, debug or extend the PostHog analytics kit (daily visitor/funnel/traffic-source snapshots from PostHog HogQL into Postgres, plus the Web chart, Web funnel and Traffic sources dashboard cards). Use when the user asks about website visitors, signups, conversion, traffic sources, AI referrals, adding a funnel event or traffic family, missing days, or PostHog numbers looking wrong (zero tonight, capped at 100 rows, 401/403). Not for blog post performance or topic weighting (use seo-engine).
- Reads credentials
- Installs packages
7 other projects use a version of this.
Adopted by
Most recently taken up by
- alinaqi/claude-bootstraptheir own version, first seen Oct 8, 2026
- lsongdev/skillstheir own version, first seen Sep 10, 2026
- SylphAI-Inc/skillstheir own version, first seen Aug 5, 2026
- alinaqi/maggytheir own version, first seen Jun 12, 2026
- CodeAlive-AI/ceo-ai-ostheir own version, first seen May 18, 2026
- Ampli-Group/agentic-mobile-blueprinttheir own version, first seen May 11, 2026
Other versions7 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 7
- alinaqi/maggy★ 711same name
- alinaqi/claude-bootstrap★ 708same name
- SylphAI-Inc/skills★ 111same name
- CodeAlive-AI/ceo-ai-os★ 9same name
- Ampli-Group/agentic-mobile-blueprint★ 4same name
- Layneformalized225/ai-cofounder★ 1same name
- lsongdev/skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository3 other files it gives its agents.
- ai-search-filesSkill
- seo-engineSkill
- wordpress-blogSkill
The file
We split this skill into the rules it gives an agent
9 rules. 0 are common, 1 is distinctive, and 8 are very specific to this project.
0
Common
Most of the 7 edited versions we read keep these.
1
Distinctive
This author's own choices. Worth a look.
8
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 18
HogQL defaults to LIMIT 100: Always set an explicit LIMIT on grouped queries.
- Very specific: line 34
Add a funnel event: add countIf(event = 'x') AS x to both HogQL queries (the snapshot and the live top-up).
- Very specific: line 35
Add a traffic family: add a rule to RULES above the generic search / direct rules.
- Very specific: line 36
Numbers look low or zero tonight: check the ET vs UTC "today" comparison.
- Very specific: line 37
Missing days: run npm run backfill.
- Very specific: line 61
Follow "Add a funnel event" above (both HogQL queries, column, upserts, getWebVisitorsDaily).
- Very specific: line 62
Run npm run backfill to fill history.
- Very specific: line 68
Check the "today" comparison uses toTimeZone(now(), 'America/New_York'), not today().
- Very specific: line 69
Run npm run snapshot and read the per-project log line.
“Common” is measured: kept by at least 6 in 10 of the 7 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
PostHog analytics kit
Critical: rules that matter
- Days are America/New_York.
- In HogQL, bucket with
toDate(toTimeZone(timestamp, 'America/New_York')). - For "today", compare against
toDate(toTimeZone(now(), 'America/New_York')), nevertoday(), which is UTC.today()makes the evening count read 0. - In JS, use
Intl.DateTimeFormat("en-CA", { timeZone: "America/New_York" }), nevertoISOString().
- In HogQL, bucket with
- HogQL defaults to LIMIT 100. Always set an explicit LIMIT on grouped queries. The sources query uses 20000.
- Visitors =
count(DISTINCT person_id)on$pageview. Sessions = distinctproperties.$session_id. - Attribute sources by session entry (
session.$entry_referring_domain,session.$entry_utm_source), not the per-pageview referrer. The per-pageview referrer is mostly your own domain after the first click. - The API key must be a personal key (
phx_) with Query: Read. A project key (phc_) returns 401/403. - In the Web chart, signups and subscribers share the visitors y-axis with the same bar width. Height shows the conversion. Never give them their own axis, or 1 sale renders as a full-height bar.
How it works
scripts/posthog-snapshot.mjsruns two HogQL queries againstPOST {host}/api/projects/{id}/query/:- Visitors, pageviews, sessions and funnel events grouped by ET day. The results go to
web_analytics_snapshots, keyed on (day, channel). - The session entry source by ET day, which goes to
web_referrer_snapshots. The source isutm:<source>when a UTM exists, else the referring domain, else$direct.
- Visitors, pageviews, sessions and funnel events grouped by ET day. The results go to
- It upserts, so re-running is always safe.
--days Nbackfills (max 400). - It always writes a heartbeat row for today, so a monitor can tell the job ran even on a zero-traffic day.
lib/posthog-client.tsgetWebVisitorsDaily()reads the table and first runs a live HogQL top-up for today. The top-up has a 90 s cooldown and a 3 s time box and fails open.lib/posthog-sources.tsgroups raw sources into families with the ordered regexRULES. The first match wins. Unknown domains show as themselves with a favicon.
Common tasks
- Add a funnel event: add
countIf(event = 'x') AS xto both HogQL queries (the snapshot and the live top-up). Add the column withalter table ... add column if not exists, add it to the upserts, then map it ingetWebVisitorsDaily. - Add a traffic family: add a rule to
RULESabove the genericsearch/directrules. - Numbers look low or zero tonight: check the ET vs UTC "today" comparison. Then run
npm run snapshotand read the per-project log line. - Missing days: run
npm run backfill. - Tracking a new site: set
POSTHOG_PROJECT_ID(single-site mode), or add a row to thechannelstable (multi-site mode).
Setup
cp .env.example .env # fill DATABASE_URL, POSTHOG_API_KEY, POSTHOG_PROJECT_ID (and POSTHOG_HOST for EU)
npm install
npm run backfill # last 90 days
npm run snapshot # then schedule at 9 AM + 9 PM ET with any cronExamples
Example 1: First setup User says: "Hook my site's PostHog data into the dashboard" Actions:
- Confirm PostHog is installed on the site and funnel events are captured.
- Fill
.env, runnpm run backfill, then schedulenpm run snapshottwice a day. Result:web_analytics_snapshotsandweb_referrer_snapshotsfilled for 90 days, cards render.
Example 2: Track a new funnel step User says: "Add trial_started to my funnel" Actions:
- Follow "Add a funnel event" above (both HogQL queries, column, upserts,
getWebVisitorsDaily). - Run
npm run backfillto fill history. Result: The new step shows in the Web funnel.
Example 3: Numbers look wrong User says: "PostHog says 0 visitors today but I know people visited" Actions:
- Check the "today" comparison uses
toTimeZone(now(), 'America/New_York'), nottoday(). - Run
npm run snapshotand read the per-project log line. Result: Evening counts match PostHog.
Troubleshooting
Error: POSTHOG_API_KEY / DATABASE_URL required
Cause: Missing env.
Solution: Fill .env from .env.example and run through the npm scripts (they pass --env-file=.env).
Error: PostHog <project> -> 401 or 403
Cause: A project key (phc_) was used, or the personal key lacks Query: Read.
Solution: Create a personal key (phx_) with Query: Read.
Symptom: tonight's numbers read 0 or low
Cause: "today" compared with UTC today().
Solution: Compare against toDate(toTimeZone(now(), 'America/New_York')).
Symptom: traffic sources stop at 100 rows Cause: HogQL defaults to LIMIT 100. Solution: Set an explicit LIMIT on grouped queries (sources uses 20000).
Symptom: most traffic shows as your own domain
Cause: Per-pageview referrer used instead of session entry.
Solution: Attribute by session.$entry_referring_domain / session.$entry_utm_source, and set SITE_DOMAIN so self-referrals group as Internal.
Symptom: missing days in charts
Cause: Snapshot job did not run those days.
Solution: npm run backfill (or --days N, max 400).
Error: SSL connection error against a local Postgres
Cause: The script connects with SSL by default.
Solution: Set PGSSL=0 for a local Postgres without SSL.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

