-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_script.php
140 lines (121 loc) · 6.54 KB
/
plugin_script.php
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
<?php
/*
Plugin Name: Mixpanel Tracker
Description: Tracks all button clicks, menu clicks, linked images, and landing page visits using Mixpanel.
Version: 1.2
Author: N.B.Ryttel
Author URI: https://github.com/ninryt
License: GPL v2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
== Privacy Notice ==
This plugin uses Mixpanel for analytics. Please ensure compliance with privacy laws (e.g., GDPR, CCPA) when using tracking features.
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
class MixpanelTracker {
private $mixpanel_token;
public function __construct() {
// Get token from WordPress options, fallback to empty string if not set
$this->mixpanel_token = get_option('mixpanel_token', '');
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('wp_footer', array($this, 'add_tracking_code'));
add_action('admin_menu', array($this, 'add_admin_menu'));
}
// Add admin menu for token configuration
public function add_admin_menu() {
add_options_page(
'Mixpanel Settings',
'Mixpanel',
'manage_options',
'mixpanel-settings',
array($this, 'settings_page')
);
}
// Create the settings page
public function settings_page() {
if (isset($_POST['mixpanel_token'])) {
update_option('mixpanel_token', sanitize_text_field($_POST['mixpanel_token']));
$this->mixpanel_token = get_option('mixpanel_token');
echo '<div class="updated"><p>Settings saved!</p></div>';
}
?>
<div class="wrap">
<h2>Mixpanel Settings</h2>
<form method="post">
<table class="form-table">
<tr>
<th scope="row">Mixpanel Token</th>
<td>
<input type="text" name="mixpanel_token" value="<?php echo esc_attr($this->mixpanel_token); ?>" class="regular-text">
<p class="description">Enter your Mixpanel project token here</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function enqueue_scripts() {
// Enqueue Mixpanel library
wp_enqueue_script('mixpanel', 'https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js', array(), null, true);
}
public function add_tracking_code() {
?>
<script>
(function(f,b){if(!b.__SV){var e,g,i,h;window.mixpanel=b;b._i=[];b.init=function(e,f,c){function g(a,d){var b=d.split(".");2==b.length&&(a=a[b[0]],d=b[1]);a[d]=function(){a.push([d].concat(Array.prototype.slice.call(arguments,0)))}}var a=b;"undefined"!==typeof c?a=b[c]=[]:c="mixpanel";a.people=a.people||[];a.toString=function(a){var d="mixpanel";"mixpanel"!==c&&(d+="."+c);a||(d+=" (stub)");return d};a.people.toString=function(){return a.toString(1)+".people (stub)"};i="disable time_event track track_pageview track_links track_forms track_with_groups add_group set_group remove_group register register_once alias unregister identify name_tag set_config reset opt_in_tracking opt_out_tracking has_opted_in_tracking has_opted_out_tracking clear_opt_in_out_tracking start_batch_senders people.set people.set_once people.unset people.increment people.append people.union people.track_charge people.clear_charges people.delete_user people.remove".split(" ");
for(h=0;h<i.length;h++)g(a,i[h]);var j="set set_once union unset remove delete".split(" ");a.get_group=function(){function b(c){d[c]=function(){call2_args=arguments;call2=[c].concat(Array.prototype.slice.call(call2_args,0));a.push([e,call2])}}var d={},e=["get_group"].concat(Array.prototype.slice.call(arguments,0));for(h=0;h<j.length;h++)b(j[h]);return d};b._i.push([e,f,c])};b.__SV=1.2;e=f.createElement("script");e.type="text/javascript";e.async=!0;e.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?
MIXPANEL_CUSTOM_LIB_URL:"file:"===f.location.protocol&&"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//)?"https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js":"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";g=f.getElementsByTagName("script")[0];g.parentNode.insertBefore(e,g)}})(document,window.mixpanel||[]);
// Initialize Mixpanel with your token
mixpanel.init('<?php echo esc_js($this->mixpanel_token); ?>', {
debug: false,
track_pageview: false,
ignore_dnt: true
});
// Track page views
mixpanel.track('Page View', {
'page': window.location.pathname,
'title': document.title,
'url': window.location.href
});
// Click event handling
document.addEventListener('click', function(e) {
let target = e.target;
// Handle clicks on icons or elements inside links
while (target && !['A', 'BUTTON'].includes(target.tagName)) {
target = target.parentElement;
}
if (!target) return; // Exit if no clickable parent found
let trackingData = {
'element_type': target.tagName,
'text': target.innerText?.trim() || target.getAttribute('aria-label') || '',
'url': target.href || window.location.href,
'id': target.id || '',
'classes': target.className || '',
'is_external': target.href ? !target.href.includes(window.location.hostname) : false
};
// For links with only icons, try to get a meaningful description
if (!trackingData.text) {
// Check for title attribute
trackingData.text = target.getAttribute('title') ||
// Check for img alt text
target.querySelector('img')?.getAttribute('alt') ||
// Check for aria-label
target.getAttribute('aria-label') ||
// Use href as last resort
target.href?.split('/').pop() || 'Unknown';
}
if (target.tagName === 'A') {
mixpanel.track('Link Click', trackingData);
} else if (target.tagName === 'BUTTON') {
mixpanel.track('Button Click', trackingData);
}
});
</script>
<?php
}
}
// Initialize the tracker
new MixpanelTracker();