Created
July 17, 2024 21:19
-
-
Save adamsilverstein/ad7cfe37b32e863a75b13a96add8131d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add Google tag to WordPress. | |
* | |
* @wordpress-plugin | |
* Plugin Name: google-tag-for-wp. | |
* Description: Add the Google tag to the header of a WordPress site. | |
* Plugin URI: n/a | |
* Version: 1.0.0 | |
* Author: Adam Silverstein, Google | |
* License: Apache License 2.0 | |
* License URI: https://www.apache.org/licenses/LICENSE-2.0 | |
*/ | |
$ga_tag = "{$ga_tag_dynamic_value}"; | |
/** | |
* This function will add the ga tag to the WordPress header. | |
*/ | |
function ga_atfwp_add_google_analytics_tag( $ga_tag ) { | |
?> | |
<!-- Google tag (gtag.js) --> | |
<script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID"></script> | |
<script> | |
window.dataLayer = window.dataLayer || []; | |
function gtag(){dataLayer.push(arguments);} | |
gtag('js', new Date()); | |
gtag('config', '<?php echo esc_attr( $ga_tag ); ?>'); | |
</script> | |
<?php | |
} | |
add_action( 'wp_head', 'ga_atfwp_add_google_analytics_tag' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment