| 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 new 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.1 |
| 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 function go() { |
| 15 |
add_filter( 'admin_init', array( __CLASS__, 'register_fields' ) ); |
| 16 |
add_action( 'wp_footer', array( __CLASS__, 'print_tag' ) ); |
| 17 |
} |
| 18 |
public static function register_fields() { |
| 19 |
register_setting( 'general', 'google_tag_manager_id', 'esc_attr' ); |
| 20 |
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' ); |
| 21 |
} |
| 22 |
public static function fields_html() { |
| 23 |
?> |
| 24 |
<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', '' ); ?>" /> |
| 25 |
<p class="description"><?php _e( 'The ID from Google’s provided code (as emphasized):', 'google_tag_manager' ); ?><br /> |
| 26 |
<code><noscript><iframe src="//www.googletagmanager.com/ns.html?id=<strong style="color:#c00;">ABC-DEFG</strong>"</code></p> |
| 27 |
<p class="description"><?php _e( 'You can get yours <a href="https://www.google.com/tagmanager/">here</a>!', 'google_tag_manager' ); ?></p> |
| 28 |
<?php |
| 29 |
} |
| 30 |
public static function print_tag() { |
| 31 |
if( ! $id = get_option( 'google_tag_manager_id', '' ) ) return; |
| 32 |
?> |
| 33 |
<!-- Google Tag Manager --> |
| 34 |
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=<?php echo $id; ?>" |
| 35 |
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> |
| 36 |
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': |
| 37 |
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], |
| 38 |
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= |
| 39 |
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); |
| 40 |
})(window,document,'script','dataLayer','<?php echo $id; ?>');</script> |
| 41 |
<!-- End Google Tag Manager --> |
| 42 |
<?php |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
google_tag_manager::go(); |