As Google says
Universal Analytics will no longer process new data in standard properties beginning July 1, 2023. Prepare now by setting up and switching over to a Google Analytics 4 property.
—
It's not worth waiting for the last minute. Also, the Google Analytics 4 offers some exciting features. If you use Universal Analytics (UA-* tracking code) in your Gatsby website, you should update your configuration.
We updated our Gatsby Starters to handle Google Analytics v4, and the below steps describe how we do this.
1. Change the Gatsby analytics plugin
This article assumes that you use the popular gatsby-plugin-google-analytics plugin. As its documentation says
Google has a guide recommending users upgrade to gtag.js instead. There is another plugin, gatsby-plugin-gtag, which uses gtag.js, and we recommend it.
—
So the first step is to replace the gatsby-plugin-google-analytics with gatsby-plugin-google-gtag.
yarn remove gatsby-plugin-google-analytics
yarn add gatsby-plugin-google-gtag
Next, replace the configuration in the "gatsby-config.js" file with new values:
module.exports = {
plugins: [{
// replace 'gatsby-plugin-google-analytics' with the new one
resolve: 'gatsby-plugin-google-gtag',
options: {
trackingIds: [
process.env.GA_MEASUREMENT_ID, // GA Measurement
],
gtagConfig: {
optimize_id: 'OPT_CONTAINER_ID',
anonymize_ip: true,
cookie_expires: 0,
},
pluginConfig: {
head: true,
respectDNT: true,
},
},
}, // ...
]
}




