Autocapture

Last updated:

|Edit this page

PostHog can capture frontend events automatically using autocapture. This captures events like pageview, screen, click, change of input, or submission associated with a button, form, input, select, or textarea.

This means you don't need to manually add tracking for individual components, links, buttons, divs, spans, or other parts of your product. Autocapture also provides data for heatmaps.

Autocapture is available in the following SDKs:

Configuring autocapture

This covers configuring autocapture in the JavaScript and React SDKs. For the React Native SDK, see our React Native docs.

Reducing events with an allow and ignorelist

Autocapture enables you to start capturing events on your site quickly, but this can lead to large numbers of events. To counteract this, you can configure autocapture using allowlists and ignorelists (NB some options use allow behavior and some use ignore behavior.

These are an array of events, URLs, elements, and CSS selectors that you want autocapture to allow or ignore.

For example, to only capture clicks on buttons on the docs section of the website that contain the data attribute ph-autocapture and don't contain the data attribute data-attr-pii="email", you can do the following:

Web
posthog.init('<ph_project_api_key>', {
api_host: 'https://us.i.posthog.com',
autocapture: {
dom_event_allowlist: ['click'], // DOM events from this list ['click', 'change', 'submit']
url_allowlist: ['posthog.com./docs/.*'], // strings or RegExps
// url_ignorelist can be used on its own, or combined with url_allowlist to further filter which URLs are captured
url_ignorelist: ['posthog.com./docs/.*/secret-section/.*'], // strings or RegExps
element_allowlist: ['button'], // DOM elements from this list ['a', 'button', 'form', 'input', 'select', 'textarea', 'label']
css_selector_allowlist: ['[ph-autocapture]'], // List of CSS selectors
element_attribute_ignorelist:['data-attr-pii="email"'], // List of element attributes to ignore
},
})

Allow and ignorelists only filter autocapture events. They don't affect the data collected by session recordings or custom events.

Preventing sensitive data capture

For security reasons and to prevent sensitive data capture, PostHog is conservative about capturing input tags. We specifically only collect the name, id, and class attributes from input tags. If you need to collect more data from inputs, you should use custom events.

If there are specific elements you don't want to capture, add the ph-no-capture class name.

HTML
<button class='ph-no-capture'>Sensitive information here</button>

Disabling autocapture

You can disable autocapture in your project settings or by setting autocapture: false in the config. If one of these is disabled, autocapture is disabled.

You can disable pageviews and pageleaves by setting capture_pageview: false. To just disable pageleaves, keep capture_pageview: true and set capture_pageleave: false.

Disabling autocapture does not disable session recording. You can disable session recording by turning it off in your project settings or using disable_session_recording: true in the config.

Note: You can update your config at runtime (e.g. to update an allowlist or disable autocapture) using the posthog.set_config() method.

For example, to update the capture_pageview config:

Web
posthog.set_config({ capture_pageview: false })

Capturing additional properties in autocapture events

If you add a data attribute onto an element in the format data-ph-capture-attribute-some-key={someValue}, then any autocapture event from that element or one of its children will have the property some-key: 'someValue' added to it. This can be useful when you want to add additional information to autocapture events.

Get the value of an element

As an example, say you have a notification bell with a value like this:

a notification bell showing 1 unread notification

You can include the unread count in the autocapture event by adding the data-ph-capture-attribute class like this:

HTML
<div
onClick={toggleNotificationsPopover}
data-ph-capture-attribute-unread-notifications-count={unreadCount}
>

The autocapture event for clicks on the bell will include the unread count as an unread-notifications-count property.

Tracking metadata

You can also attach metadata to autocapture events by adding data attributes to the element that triggers the event. This helps you track something like a customer performing a transaction (adding an item to a cart or completing a purchase).

The below ecommerce example helps you understand what users are interested in, even if they don't complete a transaction. It can also reveal which products users are interested in when correlated with information like marketing campaigns, regionality, or device type.

HTML
<button
data-ph-capture-attribute-product-id={productId}
data-ph-capture-attribute-product-name={productName}
data-ph-capture-attribute-product-price={productPrice}
data-ph-capture-attribute-product-quantity={productQuantity}
>
Add to cart
</button>

Replace the {productXx} values with the relevant information available on the webpage. Now when the Add to cart button is clicked, the autocapture event will include the product information in the event's properties, like:

JSON
properties: {
"product-id": "12345678",
"product-name": "Red t-shirt",
"product-price": "30",
"product-quantity": "1"
}

Autocaptured events

Autocaptured events include:

  • $autocapture: Clicks, inputs, typing, form submissions, and more.
  • $pageview: Triggers on page load. You might need to manually capture these in single-page apps like React, Next.js or Vue.
  • $pageleave: Triggers on onpagehide when a user navigates away from a page.
  • $rageclick: A second click within 1000ms within 30px of the first click.
  • $screen: When a user navigates on a mobile app.

You can learn more about what events are autocaptured in the JavaScript Web and React Native docs.

Note: Since pageview autocapture relies on the browser page load events, it captures the initial pageview but not subsequent ones. You might need to implement your own pageview capture using custom events. See our guides for doing this in Next.js, React, Vue, and Angular.

Autocaptured properties

Autocaptured events (and client-side custom events) have many default properties. These are distinguished by $ prefix in their name, the PostHog logo next to them in the activity tab, and the verified event logo.

You can see a full list in PostHog or in GitHub, but the most important ones include:

NameKeyExample value
Timestamp$timestamp2024-05-29T17:32:07.202Z
OS$osMac OS X
OS Version$os_version10.15.7
Browser$browserChrome
Browser Version$browser_version125
Device Type$device_typeDesktop
Current URL$current_urlhttps://example.com/page
Host$hostexample.com
Path Name$pathname/page
Screen Height$screen_height1080
Screen Width$screen_width1920
Viewport Height$viewport_height950
Viewport Width$viewport_width1903
Library$libweb
Library Version$lib_version1.31.0
Search Engine$search_enginegoogle
Referrer URL$referrerhttps://google.com
Referring Domain$referring_domainwww.google.com
Active Feature Flags$active_feature_flags['beta_feature']
Event Type$event_typeclick
UTM Source$utm_sourcenewsletter
UTM Medium$utm_mediumemail
UTM Campaign$utm_campaignproduct_launch
UTM Term$utm_termnew+product
UTM Content$utm_contentlogolink
Google Click ID$gclidTeSter-123
Google Ads Source$gad_sourcegoogle_ads
Google Search Ads 360 Click$gclsrcdsa
Google DoubleClick Click ID$dclidtestDclid123
Google Web-to-app Measure$wbraidtestWbraid123
Google App-to-web Measure$gbraidtestGbraid123
Facebook Click ID$fbclidtestFbclid123
Microsoft Click ID$msclkidtestMsclkid123
Twitter Click ID$twclidtestTwclid123
LinkedIn Ad Tracking ID$la_fat_idtestLaFatId123
Mailchimp Campaign ID$mc_cidtestMcCid123
Instagram Share Id$igshidtestIgshid123
TikTok Click ID$ttclidtestTtclid123
Plugins Succeeded$plugins_succeeded['GeoIP (56578)']
Plugins Failed$plugins_failed['plugin3']
Plugins Deferred$plugins_deferred['plugin4']
IP Address$ip192.168.1.1

Notes:

  • If enabled, GeoIP data is added also as properties.
  • Many of these are also captured as session properties.
  • Mobile libraries capture additional properties like $app_build, $app_name, $app_namespace, and $app_version.
  • These properties can be hidden in activity by checking the "Hide PostHog properties" box.

Analyzing autocaptured events and properties

Autocapture events and properties can be used like any other event type. You can use them in trends, funnels, cohorts, surveys, and more. Beyond this, they come with some special features:

  • When using the autocapture event series, you can filter by the autocaptured element's tag name, text, href target, and/or CSS selector.
Trends using autocapture properties
  • Autocapture events can be organized and renamed using actions.

  • You can query autocapture elements_chain using HogQL.

Clipboard autocapture

You can configure posthog-js to autocapture information that users copy or cut on your page with the capture_copied_text config option.

Web
posthog.init('<ph_project_api_key>', {
api_host: 'https://us.i.posthog.com',
autocapture: {
capture_copied_text: true,
// any other autocapture config
},
})

We then report the copied or cut text as a "Clipboard autocapture" event. You can use the $selected_content property in analysis or use the Activity page to view the copied content in context.

The activity view showing the copied content highlighted in context

Clipboard autocapture respects other privacy settings. For example, won't capture content from a password field.

Note: Browsers don't directly allow access to copied data for privacy reasons so when posthog-js sees a clipboard event, we capture any text currently selected in the browser.import { useSelector } from 'react-redux'

Dead clicks autocapture

A dead click (or slow click) is a click which isn't followed by a change to the page.

Dead clicks are a great way to identify opportunities to improve your UI, showing you where your users expect to be able to interact with the page but cannot.

You can collect dead clicks with the Web SDK by enabling them in PostHog settings.

Or by setting your config:

Web
posthog.init('<ph_project_api_key>', {
api_host: 'https://us.i.posthog.com',
capture_dead_clicks: true,
// any other config
})

Note: The PostHog heatmap captures dead clicks for free, collecting only the coordinates of dead clicks to display in heatmaps. Enabling the autocapture of dead clicks here allows for deeper analysis and is priced as a standard product analytics event.

Questions? Ask Max AI.

It's easier than reading through 581 docs articles.

Community questions

  • Tal
    a month ago

    How to capture element Id?

    I don't see element Id being captured in events? Why is this? How do I capture them with minimal changes to my app? I do set id="foo" for example on some things.

  • Don
    3 months ago

    Does disabling auto-capture disable pageView and pageLeave?

    I want to disable auto-capture but keep pageView and pageLeave, would this config do the trick

    PostHogProvider
    apiKey={import.meta.env.VITE_PUBLIC_POSTHOG_KEY}
    options={{
    api_host: import.meta.env.VITE_PUBLIC_POSTHOG_HOST,
    person_profiles: 'identified_only',
    autocapture: false,
    rageclick: false,
    capture_pageview: true,
    capture_pageleave: true,
    disable_session_recording: true,
    }}
    >
    {children}
    </PostHogProvider>
    • Ben
      2 months ago

      This looks good to me Don!

      Reach out if you run into any trouble.

    • Tal
      a month ago

      Hi, not Don, but how do I capture element ID? It seems to not be working for me and several other people in this comments section.

  • Ahmed
    4 months ago

    How can I resolve this CORS issue with dead-clicks-autocapture.js

    I am trying to use autocapture for my nextJS website. When the page loads, the dead-clicks-autocapture.js fails due to CORS. How can I fix this?

  • Jason
    4 months ago

    Autocapture events not available in product analytics

    We recently enable autocapture with PostHog. I see the autocapture events and can filter them as expected in the Activity>Explore page. However, when I try to make an insight chart with Product Analytics (e.g. trend, funnel), I have no option to select/filter the autocapture events.

    Any suggestions what might be missing?

  • Shavit
    4 months ago

    Autocapture by element id

    I autocapture click events in my app. I would like to view the metrics results by element ID, BUT at the moment I can set the column configuration by element text. How can I set the autocapture to capture element ID? and how can the element id be added to the element properties options?

  • Shavit
    4 months ago

    Autocapture - element ID

    Hi , I use autocapture to collect click events. at the moment I have data-attr configured in settings - autocapture I can only filter the element clicks by element text..
    What do I need to do in order to add the element id option? My app elements have the id= attribute. Do I need to change the snippet?

  • Aleksei
    6 months ago

    Autocapture of internet connection types

    Is it possible to collect info about internet connection of user? (wi-fi, 5G /4G / LTE / 3G etc.)

  • Dhaval
    7 months ago

    Intercepting the captureNativeAppLifecycleEvents

    Is there anyway to intercept the captureNativeAppLifecycleEvents? I am specifically interested to intercept the Application Installed event to push this event to GTM(Google Tag Manager).

  • landry
    7 months ago

    Autocapture + posthog.capture

    Is Autocapture automatically disabled when we add a posthog.capture("my_event") in the code or can both live alongside ?

  • What element is clicked with an Autocapture event?

    Im trying to figure out what element is clicked with an autocapture event. I have a name but I dont know really what element this is. What is the algorithm for setting a name, so I can tell my product manager on how to understand the autocapture events

    • Marcus
      7 months agoSolution

      Hey Snorre, you can access the element for each auto-capture event using the following HogQL expression: arrayElement(splitByChar('.', elements_chain), 1). Check out this guide for more details.

  • Muhammad
    8 months ago

    Element IDs are not always captured

    I have some click events that are capture but without ids for ex: I have a button with ID that contains and SVG image. what I get is svg clicked without the button id which makes displaying dashboards very hard. Any fix for that?

    • Steven(he/him)
      8 months ago

      Hey Muhammad,

      Yes, for details that are not autocaptured, check out our docs on how to capture custom events.

    • Patryk
      Edited 3 months ago

      You can style it certain way for it to show up on auto capture

      If your button has an aria-label it can be identified, otherwise you may need use "capture custom event" to identify it or show more data.

      When you have svg wrapped in a button that's when you get "clicked svg" Here is how you resolve it:

      import IconButton from '@mui/material/IconButton';
      import styled from '@emotion/styled';
      const StyledIconButton = styled(
      IconButton
      )`
      > * {
      pointer-events: none;
      `;

      That will stop the "clicked svg"

  • Zaeem
    8 months ago

    Session Event

    When a user comes to the product, is there a way to capture/filter session start and session end events with timestamps and duration between them?

  • Andrey
    10 months ago

    How to disable Autocaptured properties?

    Hey, I am working on extension, and don't want accidentally sneak and see what they are viewing, or on what pages they use my extension. Is there any way to receive only custom properties in posthog.capture()?

    • Federico
      2 months ago

      Hey Marcus I checked the link you sent to remove event properties, can you give me more help with this? I didn't achieve it with the link

  • Daniel
    a year ago

    edit autocaptured events?

    Is there a way to easily see all the things autocapture is tagging as an event? (I mean the event categories, not measured user events), and then edit them, etc?

    • Daniel
      Authora year ago

      Ok, I was just wondering if there was a way to make autocapture useful, otherwise I will create customs events.

  • Alexander
    a year ago

    Autocapture and Heat maps

    If I disable the autocaptured events, will the clicks still show on the Heat map? Or I will be able to see only the custom event clicks?

    • Marcus
      a year agoSolution

      Hey Alexander, that won't work, since the heatmap is using existing auto-captured events under the hood. It's not possible to achieve this with custom events.

  • Uri
    a year ago

    what is the event_name?

    I'm trying to optimize autocapture so my team won't have to add .capture() programmatically.

    • What's the event_name generated by autocapture? I'm guessing it's the "clicked div" events I see in PostHog
    • How to give posthog id for autocapture? e.g. ... so it will report "buy now" instead of "div".
    • Can I adjust the template for event_name?
    • What happens when clicking on a nested element? e.g. cart icon. Will the report say "clicked on svg", and not "clicked on the important 'buy now' button"?
    • Can I produce nesting? ie. report "clicked on product page > buy now" for this:
    • Marcus
      a year ago

      Hey Max, you could tag those SVG elements using data-ph-capture-attribute-some-key={"edit button"}. Check out this page for more details.

  • Alireza
    a year ago

    Effect of autocapture on heatmaps

    Would it affect heatmap if we disable autocapture or limit it for certain elements?

    • Paul(he/him)
      a year agoSolution

      Hey 👋,

      Heatmaps are built from autocapture information. So, if you disable autocapture that's the same as disabling heatmaps, exclude certain elements and they won't be represented in the heat map.

      Thanks

  • Alireza
    a year ago

    Is it possible to intercept the autocapture events and add properties to them?

    I want to add properties to autocapture events, but no with element attributes. Our usecase is an e-commerce website in which a user can select/change their store (physical store who ships the product). We want to add the currently selected store name to the autocapture events to be able to filter and analyze. For example to know how many clicks we get for different store users. Let me know if you need more details.

    • Simon(he/him)
      a year agoSolution

      Hey Alireza - Super Properties will help here. Set one when you know the store name and it will be auto-added to subsequent autocapture events.

    • Alireza
      Authora year ago

      Thank you! Stupid of me to not read all the docs!

  • Robert
    a year ago

    Reducing autocaptured properties

    The "Reducing events" section gave examples for DOM events and CSS selectors, but what about reducing autocaptured properties such as $plugins_deferred, $host, etc?

    • Marcus
      a year ago

      Hey Robert, it's not possible to remove those properties. Since you are not being charged by the amount of properties, what is your intention of reducing those?

  • Václav
    a year ago

    so how do I enable heatmaps?

    so how do I enable heatmaps?

Was this page useful?

Next article

Privacy controls

PostHog offers a range of controls to limit what data is captured by product analytics. They are listed below in order of least to most restrictive. EU-cloud hosting PostHog offers hosting on EU cloud. To use this, sign up at eu.posthog.com . If you've already created a US cloud instance and wish to migrate ticket, you must raise a support ticket in-app with the Data pipelines topic for the PostHog team to do this migration for you. This option is only available to customers on the team or…

Read next article

PostHog.com doesn't use third party cookies - only a single in-house cookie.

No data is sent to a third party.

Ursula von der Leyen, President of the European Commission