| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Wp Tracking Codes |
| 4 |
Plugin URI: https://br.wordpress.org/plugins/wp-tracking-codes/ |
| 5 |
Description: Centralize os códigos de acompanhamento em apenas um lugar. Suporte: Google Analytics, Google Adwords Remarketing, Facebook Pixel Code |
| 6 |
Version: 1.0.1 |
| 7 |
Author: Heitor Pedroso |
| 8 |
Author URI: https://github.com/heitorspedroso |
| 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.0.3'; |
| 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 |
//Load Tracking Analytics Remarketing |
| 45 |
add_action( 'wp_footer', array( $this, 'hook_analytics_remarketing' ) ); |
| 46 |
//Load Tracking Facebook ID |
| 47 |
add_action( 'wp_head', array( $this, 'hook_facebook_pixel_code' ) ); |
| 48 |
//Load Tracking Google Tag Manager after Body |
| 49 |
add_filter('template_include', array( $this, 'custom_include' ),0 ); |
| 50 |
add_filter('shutdown', array( $this, 'hook_google_tag_manager_body' ),0); |
| 51 |
} |
| 52 |
/** |
| 53 |
* Return an instance of this class. |
| 54 |
* |
| 55 |
* @return object A single instance of this class. |
| 56 |
*/ |
| 57 |
public static function get_instance() { |
| 58 |
// If the single instance hasn't been set, set it now. |
| 59 |
if ( null == self::$instance ) { |
| 60 |
self::$instance = new self; |
| 61 |
} |
| 62 |
|
| 63 |
return self::$instance; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get templates path. |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public static function get_templates_path() { |
| 72 |
return plugin_dir_path( __FILE__ ) . 'templates/'; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Load the plugin text domain for translation. |
| 77 |
*/ |
| 78 |
public function load_plugin_textdomain() { |
| 79 |
load_plugin_textdomain( 'wp-tracking-codes', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Instalation Plugin |
| 84 |
*/ |
| 85 |
public function wp_tracking_codes_install() { |
| 86 |
// Trigger our function that registers the custom post type |
| 87 |
pluginprefix_setup_post_types(); |
| 88 |
// Clear the permalinks after the post type has been registered |
| 89 |
flush_rewrite_rules(); |
| 90 |
} |
| 91 |
/** |
| 92 |
* Includes |
| 93 |
*/ |
| 94 |
private function includes() { |
| 95 |
include_once 'includes/class-register-codes.php'; |
| 96 |
} |
| 97 |
/** |
| 98 |
* Debug |
| 99 |
*/ |
| 100 |
public function log_me($message) { |
| 101 |
if (WP_DEBUG === true) { |
| 102 |
if (is_array($message) || is_object($message)) { |
| 103 |
error_log(print_r($message, true)); |
| 104 |
} else { |
| 105 |
error_log($message); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
public function hook_analytics(){ |
| 110 |
$this->options = get_option( 'tracking_option' ); |
| 111 |
if( empty( $this->options['tracking_option']['analytics'] ) && !empty( $this->options['analytics'] ) ) |
| 112 |
if($analytics = $this->options['analytics']): |
| 113 |
echo " |
| 114 |
<!-- Google Analytics Tag --> |
| 115 |
<script> |
| 116 |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
| 117 |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), |
| 118 |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
| 119 |
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); |
| 120 |
|
| 121 |
ga('create', '$analytics', 'auto'); |
| 122 |
ga('send', 'pageview'); |
| 123 |
|
| 124 |
</script> |
| 125 |
<!-- Google Analytics Tag --> |
| 126 |
"; |
| 127 |
endif; |
| 128 |
} |
| 129 |
public function hook_analytics_remarketing(){ |
| 130 |
$this->options = get_option( 'tracking_option' ); |
| 131 |
if( empty( $this->options['tracking_option']['analytics_remarketing'] ) && !empty( $this->options['analytics_remarketing'] ) ) |
| 132 |
if($analytics_remarketing = $this->options['analytics_remarketing']): |
| 133 |
echo ' |
| 134 |
<!-- Google Remarketing Tag --> |
| 135 |
<script type="text/javascript"> |
| 136 |
/* <![CDATA[ */ |
| 137 |
var google_conversion_id = '.$analytics_remarketing.'; |
| 138 |
var google_custom_params = window.google_tag_params; |
| 139 |
var google_remarketing_only = true; |
| 140 |
/* ]]> */ |
| 141 |
</script> |
| 142 |
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"></script> |
| 143 |
<noscript> |
| 144 |
<div style="display:inline;"> |
| 145 |
<img height="1" width="1" style="border-style:none;" alt="" |
| 146 |
src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/'.$analytics_remarketing.'/?value=0&guid=ON&script=0"/> |
| 147 |
</div> |
| 148 |
</noscript> |
| 149 |
<!-- Google Remarketing Tag --> |
| 150 |
'; |
| 151 |
endif; |
| 152 |
} |
| 153 |
public function hook_facebook_pixel_code(){ |
| 154 |
$this->options = get_option( 'tracking_option' ); |
| 155 |
if( empty( $this->options['tracking_option']['facebook_pixel_code'] ) && !empty( $this->options['facebook_pixel_code'] ) ) |
| 156 |
if($facebook_pixel_code = $this->options['facebook_pixel_code']): |
| 157 |
echo " |
| 158 |
<!-- Facebook Pixel Code --> |
| 159 |
<script> |
| 160 |
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod? |
| 161 |
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n; |
| 162 |
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0; |
| 163 |
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, |
| 164 |
document,'script','https://connect.facebook.net/en_US/fbevents.js'); |
| 165 |
|
| 166 |
fbq('init', '$facebook_pixel_code'); |
| 167 |
fbq('track', 'PageView');</script> |
| 168 |
<noscript><img height='1' width='1' style='display:none' |
| 169 |
src='https://www.facebook.com/tr?id=1556124141356092&ev=PageView&noscript=1' |
| 170 |
/></noscript> |
| 171 |
<!-- End Facebook Pixel Code --> |
| 172 |
"; |
| 173 |
endif; |
| 174 |
} |
| 175 |
|
| 176 |
public function custom_include($template) { |
| 177 |
ob_start(); |
| 178 |
return $template; |
| 179 |
} |
| 180 |
public function hook_google_tag_manager_body($classes){ |
| 181 |
$this->options = get_option( 'tracking_option' ); |
| 182 |
if( empty( $this->options['tracking_option']['google_tag_manager'] ) && !empty( $this->options['google_tag_manager'] ) ) |
| 183 |
if($google_tag_manager = $this->options['google_tag_manager']): |
| 184 |
$content = ob_get_clean(); |
| 185 |
$code_tag = " |
| 186 |
<!-- Google Tag Manager --> |
| 187 |
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': |
| 188 |
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], |
| 189 |
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= |
| 190 |
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); |
| 191 |
})(window,document,'script','dataLayer','$google_tag_manager');</script> |
| 192 |
<!-- End Google Tag Manager --> |
| 193 |
<!-- Google Tag Manager (noscript) --> |
| 194 |
<noscript><iframe src='https://www.googletagmanager.com/ns.html?id=$google_tag_manager' |
| 195 |
height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript> |
| 196 |
<!-- End Google Tag Manager (noscript) --> |
| 197 |
"; |
| 198 |
$content = preg_replace('#<body([^>]*)>#i',"<body$1>{$code_tag}",$content); |
| 199 |
echo $content; |
| 200 |
endif; |
| 201 |
} |
| 202 |
} |
| 203 |
add_action( 'plugins_loaded', array( 'Wp_Tracking_Codes', 'get_instance' ) ); |
| 204 |
endif; |
| 205 |
//register_deactivation_hook ( __FILE__ , 'pluginprefix_function_to_run' ); |
| 206 |
|