# Setup instructions for Google Analytics 4

**URL:** https://meta.discourse.org/t/setup-instructions-for-google-analytics-4/167863
**Category:** Data & reporting
**Tags:** analytics
**Created:** [October 21, 2020, 1:02pm UTC](https://meta.discourse.org/t/setup-instructions-for-google-analytics-4/167863 "2020-10-21T13:02:53Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![TechGeek](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techgeek/32/274321_2.png) [@TechGeek](https://meta.discourse.org/u/TechGeek)
#### Post date: [October 21, 2020, 1:02pm UTC](https://meta.discourse.org/t/setup-instructions-for-google-analytics-4/167863/1 "2020-10-21T13:02:53Z")

</div>

Google has recently released Google Analytics 4, Below are few suggestions for Discourse:

1. **Add gtag.js directly to your pages:** Add option in Dashboard to add Google Analytics 4 gtag.js directly (Yes, this can be done by embedding gtag directly in head, but for discourse additional configuration may be required). While this may take some time for development, second option can work for most meanwhile
2. **Add your Tag using Google Tag Manager:** Update existing guide at [Integrate Google Tag Manager with Google Analytics](https://meta.discourse.org/t/setup-google-tag-manager-for-analytics/47335) for Google Analytics 4
3. **Add gtag.js directly to your web pages that are already - tagged with analytics.js** : while this is an option currently, but Universal Analytics will be phased out over a period of time

---

<div class="post-metadata">

### Author: ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)
#### Post date: [October 21, 2020, 6:40pm UTC](https://meta.discourse.org/t/setup-instructions-for-google-analytics-4/167863/2 "2020-10-21T18:40:09Z")

</div>

We have this on our radar to implement and phase out `analytics.js` to `gtag.js`. We’ll update this topic when the update is ready to go.

---

<div class="post-metadata">

### Author: ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)
#### Post date: [November 6, 2020, 8:48pm UTC](https://meta.discourse.org/t/setup-instructions-for-google-analytics-4/167863/4 "2020-11-06T20:48:56Z")

</div>

We just merged an option to upgrade to GA v4 into core:

[https://github.com/discourse/discourse/commit/09b8a61f65e5a07e4fdec90ff996396b4512a40e](https://github.com/discourse/discourse/commit/09b8a61f65e5a07e4fdec90ff996396b4512a40e)

To upgrade to Universal Analytics v4 which uses `gtag` as a wrapper for `analytics.js`, head to Admin \> Settings and search for `ga_version`. Switch this setting to `v4_gtag` and refresh to upgrade to version 4.

Be aware in upgrading to v4 you will need to change any custom events you’ve defined in theme components to use the new `gtag` API instead of `ga`.

---

<div class="post-metadata">

### Author: ![Paracelsus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/paracelsus/32/164912_2.png) [@Paracelsus](https://meta.discourse.org/u/Paracelsus)
#### Post date: [November 7, 2020, 9:48am UTC](https://meta.discourse.org/t/setup-instructions-for-google-analytics-4/167863/5 "2020-11-07T09:48:36Z")

</div>

Do we need to change the `ga universal tracking code` to this new App + Web Id too or just leave it with the old UA-xxxxxxx-x id number?

---

<div class="post-metadata">

### Author: ![datvnn](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@datvnn](https://meta.discourse.org/u/datvnn)
#### Post date: [November 7, 2020, 1:39pm UTC](https://meta.discourse.org/t/setup-instructions-for-google-analytics-4/167863/6 "2020-11-07T13:39:47Z")

</div>

**Setup 1**

 ![image](https://global.discourse-cdn.com/meta/original/3X/8/4/84976d5dc4e81ee3548f59326f05426824300712.png)

**Result 1**

 ![image](https://global.discourse-cdn.com/meta/original/3X/1/5/155db4d8e2d9971e1f079cb2e186cb16904de9f2.png)

**Setup 2**

 ![image](https://global.discourse-cdn.com/meta/original/3X/f/7/f7788d2f90a37e96ac494ac896f09ab614b662b4.png)

**Result 2 - OK**

 ![image](https://global.discourse-cdn.com/meta/original/3X/9/0/904fb3278c3a3afd54029f1fd58f843e82a95d65.png)

---

<div class="post-metadata">

### Author: ![Paracelsus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/paracelsus/32/164912_2.png) [@Paracelsus](https://meta.discourse.org/u/Paracelsus)
#### Post date: [November 7, 2020, 2:53pm UTC](https://meta.discourse.org/t/setup-instructions-for-google-analytics-4/167863/8 "2020-11-07T14:53:43Z")

</div>

> [@datvnn](#):
>
> **Setup 1**
> 
> ![image](https://global.discourse-cdn.com/meta/original/3X/8/4/84976d5dc4e81ee3548f59326f05426824300712.png)
> 
> **Result 1**
> 
> ![image](https://global.discourse-cdn.com/meta/original/3X/1/5/155db4d8e2d9971e1f079cb2e186cb16904de9f2.png)
> 
> **Setup 2**
> 
> ![image](https://global.discourse-cdn.com/meta/original/3X/f/7/f7788d2f90a37e96ac494ac896f09ab614b662b4.png)
> 
> **Result 2 - OK**
> 
> ![image](https://global.discourse-cdn.com/meta/original/3X/9/0/904fb3278c3a3afd54029f1fd58f843e82a95d65.png)

Thanks. Now I got it working. 🕶

The only con so far is not having the historical data from the universal tracker on the new GA4. 😤

---

<div class="post-metadata">

### Author: ![driib](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/driib/32/203374_2.png) [@driib](https://meta.discourse.org/u/driib)
#### Post date: [February 26, 2021, 3:44pm UTC](https://meta.discourse.org/t/setup-instructions-for-google-analytics-4/167863/9 "2021-02-26T15:44:20Z")

</div>

Thank you, works well!

How can I set the following options to make tracking GDPR-compliant (w/o consent that is):

```js
    gtag("config", "XYZ", {
      anonymize_ip: true,
      client_storage: "none",
    });

```

---

<div class="post-metadata">

### Author: ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)
#### Post date: [February 26, 2021, 5:11pm UTC](https://meta.discourse.org/t/setup-instructions-for-google-analytics-4/167863/10 "2021-02-26T17:11:56Z")

</div>

You _should_ be able to add that code in via a theme component, but I haven’t tested it.

---

<div class="post-metadata">

### Author: ![TechGeek](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techgeek/32/274321_2.png) [@TechGeek](https://meta.discourse.org/u/TechGeek)
#### Post date: [June 4, 2021, 6:39am UTC](https://meta.discourse.org/t/setup-instructions-for-google-analytics-4/167863/11 "2021-06-04T06:39:51Z")

</div>

Using v4\_gtag and entering G-XXXXXXXXXX is resulting into multiple page views for single page visit.

Also is there any way to avoid inserting analytics code in admin section?

---

<div class="post-metadata">

### Author: ![j127](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j127/32/79093_2.png) [@j127](https://meta.discourse.org/u/j127)
#### Post date: [April 28, 2022, 7:24pm UTC](https://meta.discourse.org/t/setup-instructions-for-google-analytics-4/167863/12 "2022-04-28T19:24:28Z")

</div>

> [@Paracelsus](#):
>
> The only con so far is not having the historical data from the universal tracker on the new GA4. 😤

It’s possible to send data into both UA and GA4. That way there is historical data for comparison after the UA data is deleted next year.

> **[\[UA→GA4\] Send events to both your UA and GA4 properties \[Legacy\] - Analytics...](https://support.google.com/analytics/answer/11091026?hl=en)**
>
> You are viewing a legacy article about Universal Analytics. Learn more about Google Analytics 4 replacing Universal Analytics. By

I put the UA-gtag.js code snippet in `<head>` and added one more line to send data into GA4:

```javascript
gtag('config', 'UA- ********-**', { "anonymize_ip": true });
gtag('config', 'G- *********', { "anonymize_ip": true );

```

> [@TechGeek](#):
>
> but for discourse additional configuration may be required).

Does anyone know what additional configuration might be required? It seems to be working here, but my GA configuration is simple.
