Add Google analytics to Astro using Partytown

Search for a command to run...

No comments yet. Be the first to comment.
Introduction Argo Workflows is an open-source workflow orchestration tool for Kubernetes. In this tutorial, we will walk through the steps to install Argo Workflows on a Kubernetes cluster and access its server UI. Step 1: Check Current Context Befor...

The Azure CLI is a powerful tool for managing and automating tasks in Azure. To use the Azure CLI, you need to log in to your Azure account. In this blog, we will go over the different methods you can use to log in to Azure using the Azure CLI. Log i...

Connecting to an Azure VM from windows can be a bit tricky and you may get errors like WARNING: UNPROTECTED PRIVATE KEY FILE! Permissions for ' ' are too open, It is required that your private key files are NOT accessible by others This private key w...

A methodology where all the configurational and/or infrastructure codes are set up in a separate single repository is known as GitOps. This suits tools like Ansible, Terraform and Kubernetes, where they do require separate files for their configurati...

Straight to the point, there are tons of things to ask in JS, but these are a few of them which can be a quick refresher. Event loop, Single-threaded programming language - Explain, Asynchronous and synchronous, Promises and callbacks, "This" ke...

Adding google analytics to Astro can be done with the partytown integration.
Step 1: Add Partytown to your Astro site
npx astro add partytown
Step 2: Add datalayer push as a forwarding event in your astro.config.mjs file
export default defineConfig ({
integrations: [partytown({
config: {
forward: ["dataLayer.push"]
},
})],
})
Step 3: Add GA tag on every page of your Astro Note: If you are using a common layout, you can just add that there directly.
Paste the tag obtained from google analytics as soon as the head tag ends. Add type=”text/partytown” inside the script tags obtained from Google analytics.
type="text/partytown"

Screenshot of script tag placed inside the HTML after the head tag
After the above-mentioned changes, it might look something like this.
Don’t forget to add the tag to every page you have created with Astro, or add it to the common layout.
Have a great day! See you at the next one.