> For the complete documentation index, see [llms.txt](https://docs.openli.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.openli.com/docs/documentation/legaljs-widget/collecting-cookie-consent.md).

# Collect cookie consent

## Integration

{% hint style="info" %}
This guide assumes that you have successfully created your cookie-widget in the Openli app, and have its JavaScript snippet ready to integrate.
{% endhint %}

### Adding the cookie-consent widget to your site

To collect cookie-consent with legal.js, you must embed the widget using its generated JavaScript snippet.

The snippet must be included **immediately before the closing-tag of the `body` element**, like this:

```javascript
...
    ...
        <script>
            !function(){var i,e,t,s=window.legal=window.legal||[];if(s.SNIPPET_VERSION="3.0.0",i="https://widgets.legalmonster.com/v1/legal.js",!s.__VERSION__)if(s.invoked)window.console&&console.info&&console.info("legal.js: The initialisation snippet is included more than once on this page, and does not need to be.");else{for(s.invoked=!0,s.methods=["cookieConsent","document","ensureConsent","handleWidget","signup","user"],s.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);return e.unshift(t),s.push(e),s}},e=0;e<s.methods.length;e++)t=s.methods[e],s[t]=s.factory(t);s.load=function(e,t){var n,o=document.createElement("script");o.setAttribute("data-legalmonster","sven"),o.type="text/javascript",o.async=!0,o.src=i,(n=document.getElementsByTagName("script")[0]).parentNode.insertBefore(o,n),s.__project=e,s.__loadOptions=t||{}},s.widget=function(e){s.__project||s.load(e.widgetPublicKey),s.handleWidget(e)}}}();
        
            legal.widget({
                type: "cookie",
                widgetPublicKey: "Your cookie-consent widget's public-key",
            });
        </script>
    </body>
</html>
```

If you are unable to do this, you must ensure that the call to `legal.widget(...)` occurs after the DOM is ready, using a combination of the `DOMContentLoaded` event and the `document.readyState` property. [This reference article](https://javascript.info/onload-ondomcontentloaded) provides a good overview of the events involved. Contact us if you are in doubt.

### Next steps

{% content-ref url="/pages/-MG3l3FsNnDUXmu\_I24e" %}
[Blocking cookies](/docs/documentation/legaljs-widget/collecting-cookie-consent/blocking-cookies.md)
{% endcontent-ref %}

{% content-ref url="/pages/-M779BQYONQGh\_jcLPbW" %}
[Cookie-widget options](/docs/documentation/legaljs-widget/collecting-cookie-consent/cookie-widget-options.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.openli.com/docs/documentation/legaljs-widget/collecting-cookie-consent.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
