Skip to content

Commit

Permalink
Merge pull request #3 from addingwell/feature/dynamic-master-cookie
Browse files Browse the repository at this point in the history
Use ajax request to set the _aw_master_id cookie
  • Loading branch information
simcamadd authored Nov 27, 2024
2 parents be0da02 + 8995c25 commit 42508eb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ The cookie lifetime is set to 13 months.
- Contributors: Addingwell
- Tags: uuid, cookie, visitor, tracking
- Requires at least: 4.6
- Tested up to: 5.7
- Tested up to: 6.7.1
- Requires PHP: 5.6
- Stable tag: trunk
- License: GPLv2 or later
- License URI: https://www.gnu.org/licenses/gpl-2.0.html

## Installation

1. Upload the plugin files to the `/wp-content/plugins/visitor-uuid-cookie` directory, or install the plugin through the WordPress plugins screen directly.
1. Upload the plugin files to the `/wp-content/plugins/wordpress-master-cookie` directory, or install the plugin through the WordPress plugins screen directly.
2. Activate the plugin through the 'Plugins' screen in WordPress.

## Frequently Asked Questions
Expand Down
20 changes: 20 additions & 0 deletions mastercookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
document.addEventListener("DOMContentLoaded", function() {
if (!document.cookie.includes("_aw_master_id")) {
fetch(dynamic_cookie_data.ajax_url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
action: 'set_dynamic_master_cookie'
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
console.log("Cookie _aw_master_id successfully set"); // Optional: Log success message
}
})
.catch(error => console.error("Error setting the cookie _aw_master_id"));
}
});
34 changes: 22 additions & 12 deletions mastercookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,36 @@
/**
* Plugin Name: Addingwell - Visitor UUID Cookie
* Description: A simple plugin to create a visitor UUID server cookie named "_aw_master_id" with 13 months lifetime
* Version: 1.0
* Version: 1.1
* Author: Addingwell
* Author URI: https://www.addingwell.com/
*/

// Hook to initialize the UUID cookie when WordPress initializes
if (!defined('ABSPATH')) exit; // Exit if accessed directly

function set_aw_master_id() {
function dynamic_master_cookie_enqueue_script() {
wp_enqueue_script('mastercookie-js', plugin_dir_url(__FILE__) . 'mastercookie.js', null, null, true);

wp_localize_script('mastercookie-js', 'dynamic_cookie_data', array(
'ajax_url' => admin_url('admin-ajax.php'),
));
}
add_action('wp_enqueue_scripts', 'dynamic_master_cookie_enqueue_script');

// Handle AJAX request to set the cookie
function dynamic_master_cookie_ajax_handler() {
$cookieName = '_aw_master_id';
if (!isset($_COOKIE[$cookieName])) {
$cookieLifetime = time() + (60 * 60 * 24 * 30 * 13);
$domain = getMainDomain($_SERVER['SERVER_NAME']);
$cookieValue = generateUUID();
setcookie($cookieName, $cookieValue, $cookieLifetime, '/', $domain, true, true);
wp_send_json_success('Cookie set successfully.');
} else {
$cookieValue = $_COOKIE[$cookieName];
wp_send_json_success('Cookie already exists.');
}

$cookieLifetime = time() + (60 * 60 * 24 * 30 * 13);
$domain = getMainDomain($_SERVER['SERVER_NAME']);
setcookie($cookieName, $cookieValue, $cookieLifetime, '/', $domain, true, true);
wp_die(); // Always die after handling AJAX
}

function getMainDomain($url) {
$composedTlds = [
'co.uk', 'gov.uk', 'ac.uk', 'org.uk', 'net.uk', 'sch.uk', 'nhs.uk', 'police.uk',
Expand Down Expand Up @@ -65,8 +75,7 @@ function getMainDomain($url) {
// Default to last two parts if no composed TLD matches
return implode('.', array_slice($parts, -2));
}
function generateUUID()
{
function generateUUID() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xFFFF), mt_rand(0, 0xFFFF),
mt_rand(0, 0xFFFF),
Expand All @@ -76,4 +85,5 @@ function generateUUID()
);
}

add_action('init', 'set_aw_master_id');
add_action('wp_ajax_set_dynamic_master_cookie', 'dynamic_master_cookie_ajax_handler');
add_action('wp_ajax_nopriv_set_dynamic_master_cookie', 'dynamic_master_cookie_ajax_handler');

0 comments on commit 42508eb

Please sign in to comment.