Integrate PostHog

Last updated:

Once your PostHog instance is up and running, the next step is to start sending events.

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.

HTML
<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:

Tips

  • Add the snippet on your (marketing) website and your application using the same PostHog project. That means you'll be able to follow a user from the moment they come onto your website, all the way through sign up and their actual usage of your product.
  • Track users across multiple domains by including the same project snippet on all sites you wish to track.
  • Disable tracking sensitive information by adding the ph-no-capture class to elements you don't want to track. (We do this automatically for common fields like passwords and credit cards.)

Next steps

  • Add permitted domains in your PostHog project settings so you can create actions using the PostHog toolbar directly on your site - a convenient low-code option.
  • Send custom events from your codebase for things like signups, purchases, and more KPIs.

The dedicated JavaScript library page has more details about everything you can do with posthog-js.

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/json
Body:
{
"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.




Learn how to take your PostHog integrations to the next level with these in-depth guides.

Questions?

Was this page useful?

Next article

Library comparison

PostHog provides a number of both official and community maintained libraries to help you easily integrate with your preferred language or framework. This document outlines all of our current client-side and server-side libraries, as well as which features each of them currently supports. For information on how to send events using these libraries, check out this guide . Note: Session recording and autocapture are not possible in server libraries.

Read next article