PluginProbe
Wp Tracking Codes / 1.5.0
Wp Tracking Codes v1.5.0
trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.0, 1.10.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 1.9.1 1.9.2 1.9.3
wp-tracking-codes / wp-tracking-codes.php

wp-tracking-codes.php in Wp Tracking Codes 1.5.0, at wp-tracking-codes.php

238 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Wp Tracking Codes
4 Plugin URI: https://br.wordpress.org/plugins/wp-tracking-codes/
5 Description: The tracking codes in one place. Support: Google Analytics, Google Analytics 4, Google Adwords Remarketing, Google ADS Remarketing, Google Tag Manager, DataLayer Google Tag Manager for WooCommerce, Google Merchant Customer Reviews for WooCommerce, Facebook Pixel Code
6 Version: 1.5.0
7 Author: Array é Vida
8 Author URI: https://arrayevida.com.br/
9 License: GPL2
10 License URI: https://www.gnu.org/licenses/gpl-2.0.html
11 Domain Path: /languages
12 Text Domain: wp-tracking-codes
13 */
14 if(!defined('ABSPATH')){
15 exit;
16 }
17
18 if(!class_exists('Wp_Tracking_Codes')):
19 class Wp_Tracking_Codes {
20 /**
21 * Plugin version.
22 *
23 * @var string
24 */
25 const VERSION = '1.5.0';
26 /**
27 * Instance of this class.
28 *
29 * @var object
30 */
31 protected static $instance = null;
32 /**
33 * Initialize the plugin public actions.
34 */
35 private function __construct() {
36 // Load the instalation hook
37 register_activation_hook( __FILE__, 'wp_tracking_codes_install' );
38 // Load plugin text domain.
39 add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
40 //Load includes
41 $this->includes();
42 //Load Tracking Analytics
43 add_action( 'wp_head', array( $this, 'hook_analytics' ) );
44 add_action( 'wp_head', array( $this, 'hook_analytics_4' ) );
45 //Load Tracking Analytics Remarketing
46 add_action( 'wp_footer', array( $this, 'hook_analytics_remarketing' ) );
47 //Load Tracking Facebook ID
48 add_action( 'wp_head', array( $this, 'hook_facebook_pixel_code' ) );
49 //Load Tracking Google Tag Manager in Head and after Body
50 add_action('wp_head', array($this, 'hook_google_tag_manager_head'));
51 add_filter('template_include', array( $this, 'custom_include' ),0 );
52 add_filter('shutdown', array( $this, 'hook_google_tag_manager_body' ),0);
53 }
54 /**
55 * Return an instance of this class.
56 *
57 * @return object A single instance of this class.
58 */
59 public static function get_instance() {
60 // If the single instance hasn't been set, set it now.
61 if ( null == self::$instance ) {
62 self::$instance = new self;
63 }
64
65 return self::$instance;
66 }
67
68 /**
69 * Get templates path.
70 *
71 * @return string
72 */
73 public static function get_templates_path() {
74 return plugin_dir_path( __FILE__ ) . 'templates/';
75 }
76
77 /**
78 * Load the plugin text domain for translation.
79 */
80 public function load_plugin_textdomain() {
81 load_plugin_textdomain( 'wp-tracking-codes', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
82 }
83
84 /**
85 * Instalation Plugin
86 */
87 public function wp_tracking_codes_install() {
88 // Trigger our function that registers the custom post type
89 pluginprefix_setup_post_types();
90 // Clear the permalinks after the post type has been registered
91 flush_rewrite_rules();
92 }
93 /**
94 * Includes
95 */
96 private function includes() {
97 include_once 'includes/class-register-codes.php';
98 include_once 'includes/class-render-data-layer-gtm.php';
99 include_once 'includes/class-render-google-merchant.php';
100 }
101 /**
102 * Debug
103 */
104 public function log_me($message) {
105 if (WP_DEBUG === true) {
106 if (is_array($message) || is_object($message)) {
107 error_log(print_r($message, true));
108 } else {
109 error_log($message);
110 }
111 }
112 }
113 public function hook_analytics(){
114 $this->options = get_option( 'tracking_option' );
115 if( isset( $this->options['analytics'] ) && !empty( $this->options['analytics'] ) )
116 if($analytics = $this->options['analytics']):
117 echo "
118 <!-- Google Analytics Tag -->
119 <script>
120 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
121 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
122 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
123 })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
124
125 ga('create', '$analytics', 'auto');
126 ga('send', 'pageview');
127
128 </script>
129 <!-- Google Analytics Tag -->
130 ";
131 endif;
132 }
133
134 public function hook_analytics_4(){
135 $this->options = get_option( 'tracking_option' );
136 if( isset( $this->options['analytics_4'] ) && !empty( $this->options['analytics_4'] ) )
137 if($analytics_4 = $this->options['analytics_4']):
138 echo "
139 <!-- Global site tag (gtag.js) - Google Analytics -->
140 <script async src='https://www.googletagmanager.com/gtag/js?id=$analytics_4'></script>
141 <script>
142 window.dataLayer = window.dataLayer || [];
143 function gtag(){dataLayer.push(arguments);}
144 gtag('js', new Date());
145
146 gtag('config', '$analytics_4');
147 </script>
148 ";
149 endif;
150 }
151 public function hook_analytics_remarketing(){
152 $this->options = get_option( 'tracking_option' );
153 if( isset( $this->options['analytics_remarketing'] ) && !empty( $this->options['analytics_remarketing'] ) )
154 if($analytics_remarketing = $this->options['analytics_remarketing']):
155 echo '
156 <!-- Google Remarketing Tag -->
157 <script type="text/javascript">
158 /* <![CDATA[ */
159 var google_conversion_id = '.$analytics_remarketing.';
160 var google_custom_params = window.google_tag_params;
161 var google_remarketing_only = true;
162 /* ]]> */
163 </script>
164 <script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"></script>
165 <noscript>
166 <div style="display:inline;">
167 <img height="1" width="1" style="border-style:none;" alt=""
168 src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/'.$analytics_remarketing.'/?value=0&amp;guid=ON&amp;script=0"/>
169 </div>
170 </noscript>
171 <!-- Google Remarketing Tag -->
172 ';
173 endif;
174 }
175 public function hook_facebook_pixel_code(){
176 $this->options = get_option( 'tracking_option' );
177 if( isset( $this->options['facebook_pixel_code'] ) && !empty( $this->options['facebook_pixel_code'] ) )
178 if($facebook_pixel_code = $this->options['facebook_pixel_code']):
179 echo "
180 <!-- Facebook Pixel Code -->
181 <script>
182 !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
183 n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
184 n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
185 t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
186 document,'script','https://connect.facebook.net/en_US/fbevents.js');
187
188 fbq('init', '$facebook_pixel_code');
189 fbq('track', 'PageView');</script>
190 <noscript><img height='1' width='1' style='display:none'
191 src='https://www.facebook.com/tr?id=1556124141356092&ev=PageView&noscript=1'
192 /></noscript>
193 <!-- End Facebook Pixel Code -->
194 ";
195 endif;
196 }
197
198 public function custom_include($template) {
199 ob_start();
200 return $template;
201 }
202
203 public function hook_google_tag_manager_head(){
204 $this->options = get_option('tracking_option');
205 if ( isset( $this->options['google_tag_manager'] ) && !empty($this->options['google_tag_manager']))
206 if ($google_tag_manager = $this->options['google_tag_manager']) :
207 echo "
208 <!-- Google Tag Manager -->
209 <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
210 new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
211 j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
212 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
213 })(window,document,'script','dataLayer','$google_tag_manager');</script>
214 <!-- End Google Tag Manager -->
215 ";
216 endif;
217 }
218
219 public function hook_google_tag_manager_body($classes){
220 $this->options = get_option( 'tracking_option' );
221 if( isset( $this->options['google_tag_manager'] ) && !empty( $this->options['google_tag_manager'] ) )
222 if($google_tag_manager = $this->options['google_tag_manager']):
223 $content = ob_get_clean();
224 $code_tag = "
225 <!-- Google Tag Manager (noscript) -->
226 <noscript><iframe src='https://www.googletagmanager.com/ns.html?id=$google_tag_manager'
227 height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript>
228 <!-- End Google Tag Manager (noscript) -->
229 ";
230 $content = preg_replace('#<body([^>]*)>#i',"<body$1>{$code_tag}",$content);
231 echo $content;
232 endif;
233 }
234 }
235 add_action( 'plugins_loaded', array( 'Wp_Tracking_Codes', 'get_instance' ) );
236 endif;
237 //register_deactivation_hook ( __FILE__ , 'pluginprefix_function_to_run' );
238