palanticshelper/wordpress/simple-tracking.php
2025-03-31 17:01:07 +02:00

345 lines
11 KiB
PHP

<?php
/**
* Plugin Name: Simple Tracking Plugin
* Description: Adds tracking script to website header and forwards tracking data to external service
* Version: 1.0.0
* Author: Karl Breuer
* Text Domain: simple-tracking
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
class Simple_Tracking_Plugin {
// Plugin instance
private static $instance = null;
// Tracking server URL
private $tracking_server_url = 'tracking1.karlbreuer.com';
/**
* Constructor
*/
private function __construct() {
// Add the script to the header
add_action('wp_head', array($this, 'add_tracking_script'), 10);
// Register the endpoint
add_action('init', array($this, 'register_tracking_endpoint'));
// Add settings page
add_action('admin_menu', array($this, 'add_settings_page'));
add_action('admin_init', array($this, 'register_settings'));
}
/**
* Get plugin instance
*/
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Add tracking script to header
*/
public function add_tracking_script() {
$server_url = esc_url(get_option('simple_tracking_server_url', $this->tracking_server_url));
?>
<!-- Global server config -->
<script> const server = "https://<?php echo $server_url; ?>";</script>
<!-- Tracking function -->
<script>
// Initialize tracking function
function tE(eventName, additionalParams = {}) {
// Create tracking data object
console.log(eventName)
const data = {
event: eventName,
url: window.location.href,
ref: document.referrer,
ua: navigator.userAgent,
sw: window.screen.width,
lang: navigator.language || navigator.userLanguage,
};
console.log(data)
// Helper function to serialize an object into URL parameters
function serializeObject(obj, prefix) {
const str = [];
for (const p in obj) {
if (obj.hasOwnProperty(p)) {
const k = prefix ? prefix + "[" + p + "]" : p;
const v = obj[p];
if (v !== null && typeof v === "object") {
str.push(serializeObject(v, k));
} else {
str.push(encodeURIComponent(k) + "=" + encodeURIComponent(v));
}
}
}
return str.join("&");
}
// Function for fallback methods
function sendInfo() {
try {
// Send to our WordPress endpoint first, which will forward to the tracking server
const siteUrl = window.location.origin;
const trackingUrl = `${siteUrl}/track?${serializeObject(data)}`;
console.log(trackingUrl)
fetch(trackingUrl, {
method: "POST",
mode: "no-cors",
cache: "no-cache",
credentials: "omit", // Don't send cookies
headers: {
"X-Final-Destination": server // Pass the final destination
},
keepalive: true, // Ensures request completes even if page unloads
});
return true;
} catch (e) {
console.log(e)
}
}
// No sendBeacon support, use fallbacks
return sendInfo();
}
</script>
<!-- Set up page load tracking -->
<script>
document.addEventListener("DOMContentLoaded", function() {
try {
tE("pageload");
} catch (error) {
console.error("Error in DOMContentLoaded:", error);
}
});
</script>
<?php
}
/**
* Register tracking endpoint
*/
public function register_tracking_endpoint() {
add_rewrite_rule('^track/?$', 'index.php?tracking_endpoint=1', 'top');
add_rewrite_tag('%tracking_endpoint%', '1');
add_action('parse_request', array($this, 'handle_tracking_request'));
// Flush rewrite rules only on plugin activation
if (get_option('simple_tracking_flush_rules', false)) {
flush_rewrite_rules();
update_option('simple_tracking_flush_rules', false);
}
}
/**
* Handle tracking request
*/
public function handle_tracking_request($wp) {
if (isset($wp->query_vars['tracking_endpoint'])) {
// Log that we received a tracking request
error_log('Simple Tracking Plugin: Tracking request received');
// Log the query parameters
error_log('Simple Tracking Plugin: Query params: ' . print_r($_GET, true));
$this->forward_to_tracker();
exit;
}
}
/**
* Forward the request to the tracking server
*/
public function forward_to_tracker() {
// Get tracking server URL from settings
$tracking_server_url = get_option('simple_tracking_server_url', $this->tracking_server_url);
$forward_url = 'https://' . $tracking_server_url . '/spur';
// Log the request (for debugging)
error_log('Forwarding tracking request to: ' . $forward_url);
error_log('Simple Tracking Plugin: Request method: ' . $_SERVER['REQUEST_METHOD']);
// Create a new cURL resource
$ch = curl_init();
// Set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $forward_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $_SERVER['REQUEST_METHOD']);
// Get query parameters and add IP
$query = $_GET;
$query['ip'] = $this->get_real_ip();
// Build query string
$query_string = http_build_query($query);
curl_setopt($ch, CURLOPT_URL, $forward_url . '?' . $query_string);
// Prepare headers
$headers = array();
$all_headers = function_exists('getallheaders') ? getallheaders() : $this->get_all_headers();
// Forward all headers, checking for the final destination
foreach ($all_headers as $name => $value) {
if ($name == 'X-Final-Destination' && !empty($value)) {
// If a custom destination is provided in the header, use it
$forward_url = 'https://' . $value . '/spur';
curl_setopt($ch, CURLOPT_URL, $forward_url . '?' . $query_string);
error_log('Overriding destination with: ' . $value);
}
$headers[] = "$name: $value";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute request
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
error_log('cURL error: ' . curl_error($ch));
$http_code = 500;
}
// Close cURL resource
curl_close($ch);
// Send response
http_response_code($http_code);
header('Content-Type: application/json');
echo '{}';
exit;
}
/**
* Fallback implementation of getallheaders() for servers that don't have it
*/
private function get_all_headers() {
$headers = [];
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
/**
* Get the real IP address
*/
private function get_real_ip() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
/**
* Add settings page
*/
public function add_settings_page() {
add_options_page(
'Tracking Settings',
'Tracking Settings',
'manage_options',
'simple-tracking-settings',
array($this, 'render_settings_page')
);
}
/**
* Register settings
*/
public function register_settings() {
register_setting('simple_tracking_settings', 'simple_tracking_server_url');
add_settings_section(
'simple_tracking_section',
'Tracking Settings',
array($this, 'settings_section_callback'),
'simple-tracking-settings'
);
add_settings_field(
'simple_tracking_server_url',
'Tracking Server URL',
array($this, 'server_url_callback'),
'simple-tracking-settings',
'simple_tracking_section'
);
}
/**
* Settings section callback
*/
public function settings_section_callback() {
echo '<p>Configure your tracking server settings.</p>';
}
/**
* Server URL field callback
*/
public function server_url_callback() {
$value = get_option('simple_tracking_server_url', $this->tracking_server_url);
echo '<input type="text" id="simple_tracking_server_url" name="simple_tracking_server_url" value="' . esc_attr($value) . '" class="regular-text">';
echo '<p class="description">Enter the tracking server URL without https:// (e.g., tracking1.karlbreuer.com)</p>';
echo '<p class="description">You will find this in your domain screen in the Palantics app</p>';
}
/**
* Render settings page
*/
public function render_settings_page() {
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields('simple_tracking_settings');
do_settings_sections('simple-tracking-settings');
submit_button();
?>
</form>
</div>
<?php
}
/**
* Activate plugin
*/
public static function activate() {
// Set flag to flush rewrite rules on next load
update_option('simple_tracking_flush_rules', true);
}
}
// Initialize the plugin
function simple_tracking_init() {
Simple_Tracking_Plugin::get_instance();
}
add_action('plugins_loaded', 'simple_tracking_init');
// Register activation hook
register_activation_hook(__FILE__, array('Simple_Tracking_Plugin', 'activate'));