PluginProbe
Google Tag Manager / trunk
Google Tag Manager vtrunk
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 trunk, at google-tag-manager.php

134 lines 4.4 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: https://wordpress.org/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.4
7 * Author: George Stephanis
8 * Author URI: https://georgestephanis.wordpress.com
9 * License: GPLv2 or later
10 *
11 * @package Google_Tag_Manager
12 */
13
14 /**
15 * Main plugin class. Registers settings and outputs GTM snippets.
16 */
17 class Google_Tag_Manager {
18
19 /**
20 * Whether the noscript tag has already been printed on this page load.
21 *
22 * @var bool
23 */
24 public static $printed_noscript_tag = false;
25
26 /**
27 * Register all hooks.
28 *
29 * @return void
30 */
31 public static function go() {
32 add_action( 'admin_init', array( __CLASS__, 'register_fields' ) );
33 add_action( 'wp_head', array( __CLASS__, 'print_tag' ), 1 );
34 add_action( 'wp_body_open', array( __CLASS__, 'print_noscript_tag' ) ); // Core wp_body_open hook added in WP 5.2.
35 add_action( 'genesis_before', array( __CLASS__, 'print_noscript_tag' ) ); // Genesis theme framework.
36 add_action( 'tha_body_top', array( __CLASS__, 'print_noscript_tag' ) ); // Theme Hook Alliance.
37 add_action( 'body_top', array( __CLASS__, 'print_noscript_tag' ) ); // THA unprefixed variant.
38 add_action( 'wp_footer', array( __CLASS__, 'print_noscript_tag' ) ); // Fallback for themes without body-open support.
39 }
40
41 /**
42 * Register the Google Tag Manager ID setting on the General Settings page.
43 *
44 * @return void
45 */
46 public static function register_fields() {
47 register_setting(
48 'general',
49 'google_tag_manager_id',
50 array(
51 'sanitize_callback' => 'sanitize_text_field',
52 'default' => '',
53 )
54 );
55 add_settings_field( 'google_tag_manager_id', '<label for="google_tag_manager_id">' . esc_html__( 'Google Tag Manager ID', 'google-tag-manager' ) . '</label>', array( __CLASS__, 'fields_html' ), 'general' );
56 }
57
58 /**
59 * Render the Google Tag Manager ID settings field.
60 *
61 * @return void
62 */
63 public static function fields_html() {
64 ?>
65 <input type="text" id="google_tag_manager_id" name="google_tag_manager_id" placeholder="ABC-DEFG" class="regular-text code" value="<?php echo esc_attr( get_option( 'google_tag_manager_id', '' ) ); ?>" />
66 <p class="description"><?php esc_html_e( 'The ID from Google\'s provided code (as emphasized):', 'google-tag-manager' ); ?><br />
67 <code>&lt;noscript&gt;&lt;iframe src="//www.googletagmanager.com/ns.html?id=<strong style="color:#c00;">ABC-DEFG</strong>"</code></p>
68 <p class="description">
69 <?php
70 printf(
71 wp_kses(
72 /* translators: %s: URL to the Google Tag Manager sign-up page */
73 __( 'You can get yours <a href="%s">here</a>!', 'google-tag-manager' ),
74 array( 'a' => array( 'href' => array() ) )
75 ),
76 esc_url( 'https://www.google.com/tagmanager/' )
77 );
78 ?>
79 </p>
80 <?php
81 }
82
83 /**
84 * Output the Google Tag Manager JavaScript snippet in the <head>.
85 *
86 * @return void
87 */
88 public static function print_tag() {
89 $id = get_option( 'google_tag_manager_id', '' );
90 if ( ! $id ) {
91 return;
92 }
93 ?>
94 <!-- Google Tag Manager -->
95 <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
96 new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
97 j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
98 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
99 })(window,document,'script','dataLayer','<?php echo esc_js( $id ); ?>');</script>
100 <!-- End Google Tag Manager -->
101 <?php
102 }
103
104 /**
105 * Output the Google Tag Manager noscript iframe immediately after <body>.
106 *
107 * Hooked to several body-open actions to maximise theme compatibility;
108 * a static flag ensures the tag is only printed once regardless of which
109 * hook fires first.
110 *
111 * @return void
112 */
113 public static function print_noscript_tag() {
114 // Only print the noscript tag once across all the hooks we're trying.
115 if ( self::$printed_noscript_tag ) {
116 return;
117 }
118 self::$printed_noscript_tag = true;
119
120 $id = get_option( 'google_tag_manager_id', '' );
121 if ( ! $id ) {
122 return;
123 }
124 ?>
125 <!-- Google Tag Manager (noscript) -->
126 <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<?php echo esc_attr( $id ); ?>"
127 height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
128 <!-- End Google Tag Manager (noscript) -->
129 <?php
130 }
131 }
132
133 Google_Tag_Manager::go();
134