-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwp-tracking-codes.php
More file actions
executable file
·217 lines (201 loc) · 7.83 KB
/
Copy pathwp-tracking-codes.php
File metadata and controls
executable file
·217 lines (201 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* Plugin Name: Wp Tracking Codes
* Plugin URI: https://wordpress.org/plugins/wp-tracking-codes/
* Description: The tracking codes in one place. Support: GTM, GA4 Global Tag, ADS Remarketing Global Tag, Google Merchant Customer Reviews for WooCommerce, Facebook.
* Version: 1.10.0
* Requires at least: 5.2.0
* Tested up to: 7.0
* Requires PHP: 7.4
* Author: Array.codes
* Author URI: https://array.codes
* Developer: Heitor Sousa
* Developer URI: https://array.codes/
* Domain Path: /languages
* Text Domain: wp-tracking-codes
*
* WC requires at least: 4.8.0
* WC tested up to: 10.9.0
*
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
*/
if(!defined('ABSPATH')){
exit;
}
if(!class_exists('Wp_Tracking_Codes')):
class Wp_Tracking_Codes {
/**
* Plugin version.
*
* @var string
*/
const VERSION = '1.10.0';
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
* Options
*
* @var array $options Options
*/
private $options = array();
/**
* Initialize the plugin public actions.
*/
private function __construct() {
// Load the instalation hook
register_activation_hook( __FILE__, 'wp_tracking_codes_install' );
// Load plugin text domain.
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
//Load includes
$this->includes();
//Load Tracking Analytics
add_action( 'wp_head', array( $this, 'hook_analytics_4' ) );
//Load Tracking Analytics Remarketing
add_action( 'wp_footer', array( $this, 'hook_analytics_remarketing' ) );
//Load Tracking Facebook ID
add_action( 'wp_head', array( $this, 'hook_facebook_pixel_code' ) );
//Load Tracking Google Tag Manager in Head and after Body
add_action('wp_head', array($this, 'hook_google_tag_manager_head'));
add_filter('wp_body_open', array( $this, 'hook_google_tag_manager_body' ),0);
add_action( 'before_woocommerce_init', array( $this, 'declare_compatibility_hpos') );
}
/**
* Return an instance of this class.
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Get templates path.
*
* @return string
*/
public static function get_templates_path() {
return plugin_dir_path( __FILE__ ) . 'templates/';
}
/**
* Load the plugin text domain for translation.
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'wp-tracking-codes', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Instalation Plugin
*/
public function wp_tracking_codes_install() {
// Trigger our function that registers the custom post type
pluginprefix_setup_post_types();
// Clear the permalinks after the post type has been registered
flush_rewrite_rules();
}
/**
* Includes
*/
private function includes() {
include_once 'includes/class-register-codes.php';
include_once 'includes/class-render-google-merchant.php';
}
public function hook_analytics_4(){
$this->options = get_option( 'tracking_option' );
if( isset( $this->options['analytics_4'] ) && !empty( $this->options['analytics_4'] ) ){
$analytics_4 = $this->options['analytics_4'];
printf("
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src='https://www.googletagmanager.com/gtag/js?id=%s'></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '%s');
</script>
", esc_attr($analytics_4), esc_attr($analytics_4));
}
}
public function hook_analytics_remarketing() {
$this->options = get_option( 'tracking_option' );
if( isset( $this->options['analytics_remarketing'] ) && !empty( $this->options['analytics_remarketing'] ) ){
$analytics_remarketing = $this->options['analytics_remarketing'];
printf("
<!-- Global site tag (gtag.js) - Google Ads -->
<script async src='https://www.googletagmanager.com/gtag/js?id=%s'></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '%s');
</script>
", esc_attr($analytics_remarketing), esc_attr($analytics_remarketing));
}
}
public function hook_facebook_pixel_code() {
$this->options = get_option( 'tracking_option' );
if ( isset( $this->options['facebook_pixel_code'] ) && ! empty( $this->options['facebook_pixel_code'] ) ){
$facebook_pixel_code = $this->options['facebook_pixel_code'];
printf( "
<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '%1\$s');
fbq('track', 'PageView');</script>
<noscript><img height='1' width='1' style='display:none'
src='https://www.facebook.com/tr?id=%1\$s&ev=PageView&noscript=1'
/></noscript>
<!-- End Facebook Pixel Code -->
", esc_attr($facebook_pixel_code) );
}
}
public function hook_google_tag_manager_head(){
$this->options = get_option('tracking_option');
if ( isset( $this->options['google_tag_manager'] ) && !empty($this->options['google_tag_manager'])){
$google_tag_manager = $this->options['google_tag_manager'];
printf(
"<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','%s');</script>
<!-- End Google Tag Manager -->",
esc_attr( $google_tag_manager )
);
}
}
public function hook_google_tag_manager_body($classes){
$this->options = get_option( 'tracking_option' );
if( isset( $this->options['google_tag_manager'] ) && !empty( $this->options['google_tag_manager'] ) ){
$google_tag_manager = $this->options['google_tag_manager'];
printf("<!-- Google Tag Manager (noscript) -->
<noscript><iframe src='https://www.googletagmanager.com/ns.html?id=%s' height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->",
esc_attr( $google_tag_manager )
);
}
}
/**
* Declare_compatibility_hpos
*/
public function declare_compatibility_hpos() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', 'wp-tracking-codes/wp-tracking-codes.php', true );
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', 'wp-tracking-codes/wp-tracking-codes.php', true );
}
}
}
add_action( 'plugins_loaded', array( 'Wp_Tracking_Codes', 'get_instance' ) );
endif;