Integrazione di Google Tag Manager con Google Analytics

:bookmark: This guide will help you set up Google Tag Manager and use it with Google Analytics on your Discourse site. Additionally, it covers configuring variables, triggers, and tags in Google Tag Manager to track page views.

:information_source: This guide covers setting up Google Tag Manager. If you only want to enable Google Analytics for your site, set the G-Tag in the ga universal tracking code site setting. Our settings may still refer to all analytics as universal, but this is a legacy field naming artifact. The following instructions use, and work for, Google Tags.

:person_raising_hand: Required user level: Administrator

Integrating Google Tag Manager (GTM) with Google Analytics (GA) allows you to manage and track events on your Discourse site effectively. This guide covers the full setup process, from creating a GA account to configuring GTM with the necessary tags and triggers.

Summary

  1. Create a Google Analytics account and property.
  2. Set up Google Tag Manager.
  3. Configure GTM variables, triggers, and tags.
  4. Integrate GTM with your Discourse site.

Create a Google Analytics account

  1. Visit Google Analytics and sign in with your Google account.

  2. If you don’t have an account yet, follow the guided steps to create one.

  3. Create a property by clicking on “Admin” and then “Create Property”.

  4. Fill in the required details on the “Property creation” screen.

  5. Enter your business details on the next screen.

  6. Select your business objectives appropriately.

:information_source: Info: Selecting the four sections will most closely match the reports Analytics has typically provided:


7. Choose “Web” as the platform for data collection.

  1. Set up a data stream by entering your site’s URL and a stream name.

:information_source: After successfully creating your web stream, make a note of your Measurement ID. You’ll need this for GTM setup.

Set up Google Tag Manager

  1. Visit Google Tag Manager and create an account.
  2. Set up a container by following the guided steps, and choose “Web” as the platform.

Configure GTM

Variables

  1. Go to the “Variables” tab in GTM and click “New” in the User-Defined Variables section.
  2. Create two Data Layer Variables:
  • Title: DL-page-title
    Type: Data Layer Variable
    Data Layer Variable Name: page.title
    Data Layer Version: 2

  • Title: DL-page-url
    Type: Data Layer Variable
    Data Layer Variable Name: page.url
    Data Layer Version: 2

As an example, choose the variable type:

Then configure the variables, as in this example:

Triggers

  1. Go to the “Triggers” tab and click “New”.

  2. Choose “Custom Event” as the type.

  3. Name the trigger virtualPageView and Save it.

    The end result should look like this:

Tags

  1. Go to the “Tags” tab and create a new tag.

  2. Choose Google Analytics: Google Tag as the tag type.

  3. Configure the tag with the following details:

  • Measurement ID: Enter your Measurement ID from GA.
  • Required Fields to Set:
    • title: {{DL-page-title}}
    • page: {{DL-page-url}}
    • send_page_view: true
    • page_path: {{Page Path}}

When finished it should look like this (note that you can add a userID parameter in addition to the parameters above, but you will need to add an additional variable like DL-page-url.):

  1. Choose the virtualPageView trigger for this tag.
  2. Name your tag and Save it.

Publish your GTM workspace

Publish your container in GTM by clicking the blue “Submit” button in the upper-right corner of the page.

:warning: Important: If you encounter 404 errors from googletagmanager.com, ensure your GTM container changes have been published.

Integrate GTM with Discourse

  1. Go to your Discourse Admin settings and search for gtm container id.
  2. Enter your GTM Container ID (format: GTM-XXXXXX) in the corresponding field.

:warning: Note: Remove any existing Analytics tracking code from the ga universal tracking code and ga tracking code settings in Discourse.

Reload your site and check the Real-Time content view in GA to confirm that data is flowing through GTM.

Additional Uses

With GTM, you can add more tags to track additional events such as:

Related Links

Last edited by @jessii 2024-08-03T01:04:37Z

Check documentPerform check on document:
46 Mi Piace

Come notato, il tag di configurazione GA4 è stato rimosso e ora sceglierai il Tag di Google. I passaggi di configurazione rimangono gli stessi, ma ho aggiornato la documentazione.

Per chiunque avesse precedentemente configurato il tag GA4, questo dovrebbe essere stato aggiornato automaticamente e non richiedere modifiche da parte tua.

Vale la pena menzionare che questa non è una configurazione ottimale in termini di best practice GA4/GTM attuali e il passaggio n. 2 nella sezione Trigger sembra essere errato.

Invece di un trigger Page View, questo dovrebbe essere un trigger Custom Event con il nome virtualPageView.

cookieDomain = auto sembra essere un residuo di Universal Analytics e non è necessario. GA4 gestisce automaticamente la navigazione tra sottodomini.

Un’altra best practice è far sì che il Google Tag venga attivato all’inizializzazione, impostare send_page_view su false e avere invece un tag GA4 Event separato per page_view.

Quando abbiamo implementato, abbiamo anche aggiunto un override per page_location con l’URL completo del sito + il percorso della pagina virtuale. Ciò popolerà correttamente i report GA4 standard.

1 Mi Piace

Ciao Stefan,

Grazie per queste note.

Ho aggiornato il passaggio #2 della sezione Trigger poichĂŠ avevo giĂ  screenshot corrispondenti e aggiorneremo il resto il prima possibile.

2 Mi Piace

Descrizione:

Sto riscontrando un problema con Google Tag Manager (GTM) sul mio forum Discourse. Ecco una spiegazione dettagliata della mia configurazione e del problema:

  1. Configurazione GTM:

    • Ho configurato un container di Google Tag Manager (ID: GTM-…MZV).
    • A scopo di test, ho aggiunto un tag Google Analytics 4 in GTM, configurato per attivarsi in tutte le pagine durante l’inizializzazione.
    • Non ho aggiunto trigger complessi, user layer o data layer. È solo una configurazione di base per verificare che GTM funzioni.
  2. Integrazione GTM con Discourse:

    • Nel mio forum Discourse, ho configurato GTM inserendo l’ID del container (GTM-W…MZV) nelle Impostazioni di amministrazione.
    • Quando ispeziono il mio sito web, posso vedere che:
  3. Il Problema:

    • Dopo aver pubblicato il mio container GTM, sono andato in ModalitĂ  Anteprima in Google Tag Manager e ho inserito l’URL del mio sito web (https://forum.hobiguru.com).
    • Tuttavia, il Debugger di Anteprima GTM indica che nessun tag GTM è stato trovato sul mio sito.
    • Nonostante gtm.js venga caricato e non ci siano errori visibili, il debugger non riconosce la configurazione GTM.

Screenshot:

  • Screenshot 1: Configurazione del container GTM in Google Tag Manager (mostra il tag GA4 e i trigger).
  • Screenshot 2: Impostazioni di amministrazione di Discourse che mostrano l’ID del container GTM (GTM-W…MZV) configurato.
  • Screenshot 3: Strumenti per sviluppatori del browser che mostrano il caricamento corretto della libreria gtm.js sul mio sito web.
  • Screenshot 4: ModalitĂ  Anteprima GTM che mostra il messaggio di errore: Nessun tag GTM trovato sul tuo sito web.



Cosa ho provato finora:

  • Ho confermato che gtm.js viene caricato nella scheda Network.
  • Ho controllato la console del browser per eventuali errori di Content Security Policy (CSP) o altri errori JavaScript.
  • Ho cancellato la cache del mio sito e del browser per assicurarmi che non ci siano problemi di caching.
  • Ho ripubblicato il container GTM per assicurarmi che sia aggiornato.

Richiesta:

Qualcuno può aiutarmi a capire perché la Modalità Anteprima GTM non rileva la mia configurazione GTM, anche se lo script gtm.js viene caricato correttamente? C’è qualcosa che mi sfugge nella configurazione o nel processo di integrazione?