> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://help.cookiewow.com/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# How to Configure Google Analytics to Collect Anonymous User Statistics Before Consent (For Websites Using HTML Integration)

**How to Configure Google Analytics to Collect Anonymous User Statistics Before Consent (For Websites Using HTML Integration)**

> If your website uses **Google Tag Manager** instead of HTML for Google Analytics, please follow the setup instructions [here](https://help.cookiewow.com/th/article/google-analytics-anonymous-ga-html-qvu3bf/).
---

### Note:
If your website collects personally identifiable information for analytics or marketing, you are required by PDPA to obtain user consent **before** any data is collected.  
However, if your goal is simply to track actual visitor numbers — including those who haven’t given consent — enabling **anonymous tracking** helps reduce compliance risks while still providing useful insights.
---

## Concept:

Configure Google Analytics to operate in **anonymized** mode before the user has given consent. This means:

* No cookies are stored  
* IP addresses are anonymized  
* Statistics remain rough but functional  

Once the user **accepts analytic cookies**, the script is allowed to operate **normally** again (using cookies).
---

## Instructions:

### 1. Move Google Tags to the "Necessary" category in Cookie Manager

In your cookie management settings, move these scripts from **Analytics** to **Necessary**:

```html
https://www.googletagmanager.com/gtm.js?id=<GTM-KEY>
https://www.google-analytics.com/analytics.js
```

This allows them to load on all visits (but in restricted mode before consent).
---

### 2. Modify Your Google Analytics Script in HTML

Here’s the default format of a Google Analytics (gtag.js) script:

```html
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', '<GA_MEASUREMENT_ID>');
</script>
```

Update it to include consent detection like this:  
(Replace `'category-slug'` with the actual cookie category slug, such as `'analytics'`. [How to find a slug](https://help.cookiewow.com/th/article/slug-199e1ow/))

```html
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  if (window.cwcIsUserAccept && window.cwcIsUserAccept('category-slug')) {
    // User has given consent → use normal tracking
    gtag('config', '<GA_MEASUREMENT_ID>');
  } else {
    // No consent yet → use anonymous mode
    gtag('config', '<GA_MEASUREMENT_ID>', {
      client_storage: 'none',
      anonymize_ip: true
    });
  }
</script>
```
---

### Explanation of the Parameters:
* `client_storage: 'none'` → disables cookies  
* `anonymize_ip: true` → masks the user's IP  
* `cwcIsUserAccept('category-slug')` → checks if user consent exists using the Cookie Wow script
---

With this setup, your website can:
* Track rough visitor metrics even before consent  
* Comply with privacy regulations  
* Enable full analytics only after explicit user approval

For more information about `gtag` configuration options, visit: [Google Analytics gtag.js documentation](https://developers.google.com/analytics/devguides/collection/gtagjs/cookies-user-id)  
