How to implement GA4 on the WordPress site without plugins?
To implement Google Analytics 4 (GA4) on a WordPress site without plugins, you can follow these general steps:
- Create a GA4 property: First, you need to create a new GA4 property in your Google Analytics account. Follow the steps in Google Analytics to create a new property and get your GA4 measurement ID.
- Add the GA4 tracking code to your WordPress site: To add the GA4 tracking code to your WordPress site, you need to edit your theme files. First, copy the GA4 tracking code provided by Google Analytics. Then, open the header.php file of your WordPress theme and paste the tracking code before the closing </head> tag.
- Add a script for sending pageviews: After adding the GA4 tracking code to your site, you need to add a script to send pageview data to Google Analytics. Add the following script to your theme’s functions.php file:
add_action( 'wp_head', 'send_pageview_to_google_analytics' );
function send_pageview_to_google_analytics() {
?>
<script>
gtag('event', 'page_view', {
'send_to': 'GA_MEASUREMENT_ID',
'page_title': '<?php echo wp_title(); ?>',
'page_location': '<?php echo get_permalink(); ?>',
'page_path': '<?php echo $_SERVER['REQUEST_URI']; ?>'
});
</script>
<?php
}
Make sure to replace “GA_MEASUREMENT_ID” with your GA4 measurement ID that you obtained in step 1.
- Test your implementation: After completing the above steps, you should test your implementation to ensure that GA4 is properly tracking data on your WordPress site. You can use the Realtime reports in Google Analytics to confirm that data is being recorded correctly.
Note that the above steps are general guidelines and may vary depending on the specific WordPress theme and setup you are using. It’s always a good idea to consult with a web developer or refer to Google’s documentation for more detailed instructions.
*The information contained in this post is for general information purposes only. The information is provided by How to implement GA4 on the Wordpress site without plugins? and while we endeavor to keep the information up to date and correct, we make no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services, or related graphics contained on the post for any purpose.