| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Google Tag Manager |
| 4 |
Plugin URI: http://wordpress.org/extend/plugins/google-tag-manager/ |
| 5 |
Description: This is an implementation of the Tag Management system from Google. It adds a field to the existing General Settings page for the ID, and if specified, outputs the tag management javascript in the page footer. |
| 6 |
Version: 1.0.3 |
| 7 |
Author: George Stephanis |
| 8 |
Author URI: http://stephanis.info |
| 9 |
License: GPLv2 or later |
| 10 |
*/ |
| 11 |
|
| 12 |
class google_tag_manager { |
| 13 |
|
| 14 |
public static $printed_noscript_tag = false; |
| 15 |
|
| 16 |
public static function go() { |
| 17 |
add_filter( 'admin_init', array( __CLASS__, 'register_fields' ) ); |
| 18 |
add_action( 'wp_head', array( __CLASS__, 'print_tag' ) ); |
| 19 |
add_action( 'wp_body_open', array( __CLASS__, 'print_noscript_tag' ) ); // Core's new implementation! https://make.wordpress.org/themes/2019/03/29/addition-of-new-wp_body_open-hook/ |
| 20 |
add_action( 'genesis_before', array( __CLASS__, 'print_noscript_tag' ) ); // Genesis |
| 21 |
add_action( 'tha_body_top', array( __CLASS__, 'print_noscript_tag' ) ); // Theme Hook Alliance |
| 22 |
add_action( 'body_top', array( __CLASS__, 'print_noscript_tag' ) ); // THA Unprefixed |
| 23 |
add_action( 'wp_footer', array( __CLASS__, 'print_noscript_tag' ) ); // Fallback! |
| 24 |
} |
| 25 |
public static function register_fields() { |
| 26 |
register_setting( 'general', 'google_tag_manager_id', 'esc_attr' ); |
| 27 |
add_settings_field( 'google_tag_manager_id', '<label for="google_tag_manager_id">' . __( 'Google Tag Manager ID' , 'google_tag_manager' ) . '</label>' , array( __CLASS__, 'fields_html') , 'general' ); |
| 28 |
} |
| 29 |
public static function fields_html() { |
| 30 |
?> |
| 31 |
<input type="text" id="google_tag_manager_id" name="google_tag_manager_id" placeholder="ABC-DEFG" class="regular-text code" value="<?php echo get_option( 'google_tag_manager_id', '' ); ?>" /> |
| 32 |
<p class="description"><?php _e( 'The ID from Google’s provided code (as emphasized):', 'google_tag_manager' ); ?><br /> |
| 33 |
<code><noscript><iframe src="//www.googletagmanager.com/ns.html?id=<strong style="color:#c00;">ABC-DEFG</strong>"</code></p> |
| 34 |
<p class="description"><?php _e( 'You can get yours <a href="https://www.google.com/tagmanager/">here</a>!', 'google_tag_manager' ); ?></p> |
| 35 |
<?php |
| 36 |
} |
| 37 |
public static function print_tag() { |
| 38 |
if ( ! $id = get_option( 'google_tag_manager_id', '' ) ) return; |
| 39 |
?> |
| 40 |
<!-- Google Tag Manager --> |
| 41 |
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': |
| 42 |
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], |
| 43 |
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= |
| 44 |
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); |
| 45 |
})(window,document,'script','dataLayer','<?php echo esc_js( $id ); ?>');</script> |
| 46 |
<!-- End Google Tag Manager --> |
| 47 |
<?php |
| 48 |
} |
| 49 |
public static function print_noscript_tag() { |
| 50 |
// Make sure we only print the noscript tag once. |
| 51 |
// This is because we're trying for multiple hooks. |
| 52 |
if ( self::$printed_noscript_tag ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
self::$printed_noscript_tag = true; |
| 56 |
|
| 57 |
if ( ! $id = get_option( 'google_tag_manager_id', '' ) ) return; |
| 58 |
?> |
| 59 |
<!-- Google Tag Manager (noscript) --> |
| 60 |
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<?php echo esc_attr( $id ); ?>" |
| 61 |
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> |
| 62 |
<!-- End Google Tag Manager (noscript) --> |
| 63 |
<?php |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
google_tag_manager::go(); |
| 68 |
|