# gtag.js consent management

This composable utilizes the createGtag method and manages cookie detection, automatic page reloads, and plugin instantiation.

### Usage

```typescript
import { configure, useConsent } from 'vue-gtag'

configure({
  tagId: 'GA_MEASUREMENT_ID',
  initMode: 'manual'
})

const { hasConsent, acceptAll, rejectAll, acceptCustom } = useConsent()
```

{% hint style="warning" %}
Remember to set "initMode" to "manual" when using this composable to avoid initializing twice.
{% endhint %}

### Features

* Automatic consent detection based on GA cookie presence
* Methods to accept/reject all tracking features
* Custom consent management for granular control
* Automatic page reload after consent changes to ensure proper tracking state

### API

#### Return Values

| Name           | Type                                  | Description                                                        |
| -------------- | ------------------------------------- | ------------------------------------------------------------------ |
| `hasConsent`   | `Ref<boolean>`                        | Reactive reference indicating if user has previously given consent |
| `acceptAll`    | `() => void`                          | Grants consent for all tracking features                           |
| `rejectAll`    | `() => void`                          | Denies consent for all tracking features                           |
| `acceptCustom` | `(params: GtagConsentParams) => void` | Updates consent for specific tracking features                     |

#### Example

```javascript
// main.ts
import { createGtag } from "vue-gtag";

createGtag({
  tagId: 'GA_MEASUREMENT_ID',
  initMode: 'manual'
})
```

```html
<script setup lang="ts">
// component.vue
import { useConsent } from "vue-gtag";

const { acceptAll, rejectAll, hasConsent } = useConsent();
</script>

<template>
  <div v-if="hasConsent">I have consent</div>
  <div v-else>
    <button @click="rejectAll">Reject all</button>
    <button @click="acceptAll">Accept All</button>
  </div>
</template>
```


---

# Agent Instructions: 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:

```
GET https://matteo-gabriele.gitbook.io/vue-gtag/gtag.js-consent-management.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
