Once your PostHog instance is up and running, the next step is to get PostHog installed.
Tip: Even if you have multiple customer-facing products e.g. a marketing website + iOS app + web app, it works best to have them share the same project to track the user across their journey. Learn more
Installation
JavaScript snippet Recommended
This is the simplest way to get PostHog up and running on your website, and only takes a few minutes to set-up.
Add to your website & app
Paste this snippet within the <head>
tags of your website - ideally just inside the closing </head>
tag - on all pages that you wish to track.
<script>!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".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);posthog.init('<ph_project_api_key>', {api_host: '<ph_instance_address>'})</script>
Be sure to replace <ph_project_api_key>
and <ph_instance_address>
with your project's values. (You can find the snippet pre-filled with this data in the PostHog app under Project / Settings. (Quick links if you use PostHog Cloud US or PostHog Cloud EU)
What this code does
After adding the snippet to your website, it will automatically start to:
- Capture
$pageview
events when a user visits a page - Track when users click on links or buttons
- Record videos of user sessions that you can play back (if you've enabled the Session recordings feature, which we highly recommend!)
Custom-built libraries for integrating PostHog with popular client and server-side langauges.
Framework-specific guides that cover our recommended approach to installing PostHog in a number of popular environments.
Events can be ingested directly using our API and the /capture
endpoint, which is the same endpoint that all of our libraries use behind the scenes.
Generally, this isn't something you'll need to use when integrating PostHog, but if you're working with a language or framework that PostHog doesn't support yet, this will allow you to still send events.
Note: For this API, you should use your 'Project API Key' from the 'Project' page in PostHog. This is the same key used in your frontend snippet.
Sending events
Events can be sent either one at a time, or together in a batch. There is no limit on the number of events you can send in a batch, but the entire request body must be less than 20MB
by default.
POST https://[your-instance].com/capture/Content-Type: application/jsonBody:{"api_key": "<ph_project_api_key>","event": "[event name]","properties": {"distinct_id": "[your users' distinct id]","key1": "value1","key2": "value2"},"timestamp": "[optional timestamp in ISO 8601 format]"}
Note: Timestamp is optional. If not set, it'll automatically be set to the current time.
For more information, read our API docs.