Skip to content

Instantly share code, notes, and snippets.

@Kernix13
Created March 30, 2024 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kernix13/5bf5506a14dea458a8bbb0885de6ea55 to your computer and use it in GitHub Desktop.
Save Kernix13/5bf5506a14dea458a8bbb0885de6ea55 to your computer and use it in GitHub Desktop.
Google Analytics loaded via wp_print_scripts
/*
DELETE THIS COMMENT BLOCK
1. replace 'yourtheme' in yourtheme_google_print_scripts with your theme name
- it occurs twice in the code below
2. replace the URL below that ends with 'ABC123' with your URL
3. replace 'ABC123' argument below in the 'gtag' function
4. Paste all of the code to the bottom of your functions.php file in the child theme or custom theme
*/
function yourtheme_google_print_scripts() { ?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ABC123"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-ABC123');
</script>
<?php
}
add_action('wp_print_scripts', 'yourtheme_google_print_scripts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment