PluginProbe
Google Tag Manager / 1.0.1
Google Tag Manager v1.0.1
trunk 1.0.1 1.0.2 1.0.2-beta1 1.0.3 1.0.4
google-tag-manager / google-tag-manager.php

google-tag-manager.php in Google Tag Manager 1.0.1, at google-tag-manager.php

46 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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&rsquo;s provided code (as emphasized):', 'google_tag_manager' ); ?><br />
26 <code>&lt;noscript&gt;&lt;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();