<?php
|
namespace JVBase\managers;
|
|
if (!defined('ABSPATH')) {
|
exit; // Exit if accessed directly
|
}
|
|
/**
|
* Class for building and managing umami.js tracking data attributes
|
*/
|
class UmamiTracker
|
{
|
private array $valid_events = [
|
'view_feed',
|
'view_taxonomy',
|
'view_profile',
|
'view_shop',
|
'view_content',
|
'toggle_favourite',
|
'click_profile',
|
'click_content',
|
'click_taxonomy',
|
'click_shop',
|
];
|
private array $valid_types;
|
private array $context;
|
|
/**
|
* Constructor
|
*
|
* @param array $context Optional context data to use for all tracking
|
*/
|
public function __construct(array $context = [])
|
{
|
$this->context = $context;
|
global $jvb_content;
|
$this->valid_types = array_keys($jvb_content);
|
|
|
// Add current page information if available
|
if (empty($this->context['source_id']) && is_singular()) {
|
$this->context['source_id'] = get_the_ID();
|
$this->context['source_type'] = get_post_type();
|
} elseif (empty($this->context['source_id']) && is_tax()) {
|
$obj = get_queried_object();
|
$this->context['source_id'] = $obj->term_id;
|
$this->context['source_type'] = $obj->taxonomy;
|
}
|
}
|
|
/**
|
* Build tracking attributes for a specific event
|
*
|
* @param string $event Event type
|
* @param string $type Content type
|
* @param array $args Additional arguments
|
*
|
* @return array/string HTML attributes for tracking
|
*/
|
public function buildAttributes(string $event, string $type, array $args = []):array
|
{
|
// Validate event and type
|
if (!in_array($event, $this->valid_events)) {
|
return [];
|
}
|
|
if (!in_array($type, $this->valid_types)) {
|
return [];
|
}
|
|
// Merge with context
|
$args = array_merge($this->context, $args);
|
|
// Normalize type (remove jvb_ prefix)
|
$normalized_type = str_replace(BASE, '', $type);
|
|
// Start building attributes
|
$attributes = [
|
'umamiEvent' => esc_attr($event),
|
'umamiEventType' => esc_attr($normalized_type)
|
];
|
|
// Add content ID if available
|
if (!empty($args['id'])) {
|
$attributes['umamiEventId'] = esc_attr($args['id']);
|
}
|
|
// Add source info (where the action originated)
|
if (!empty($args['source_id'])) {
|
$attributes['umamiEventSource'] = esc_attr($args['source_id']);
|
|
if (!empty($args['source_type'])) {
|
$attributes['umamiEventSourceType'] = esc_attr(
|
str_replace(BASE, '', $args['source_type'])
|
);
|
}
|
}
|
|
// Add owner info (usually artist or partner)
|
if (!empty($args['owner_id'])) {
|
$attributes['umamiEventOwner'] = esc_attr($args['owner_id']);
|
|
if (!empty($args['owner_type'])) {
|
$attributes['umamiEventOwnerType'] = esc_attr(
|
str_replace(BASE, '', $args['owner_type'])
|
);
|
}
|
}
|
|
// Add referrer (how the user got here)
|
if (!empty($args['from'])) {
|
$attributes['umamiEventFrom'] = esc_attr($args['from']);
|
}
|
|
// Add item (used in views to see what people are clicking on)
|
if (!empty($args['item'])) {
|
$attributes['umamiEventItem'] = esc_attr($args['item']);
|
}
|
|
// Add any additional metadata
|
if (!empty($args['meta']) && is_array($args['meta'])) {
|
foreach ($args['meta'] as $key => $value) {
|
$attributes['umamiEventMeta' . ucFirst(str_replace('-', '', sanitize_key($key)))] = esc_attr($value);
|
}
|
}
|
|
return $attributes;
|
}
|
|
/**
|
* Build tracking attributes for content clicks
|
*
|
* @param int $id Content ID
|
* @param string $type Content type
|
* @param array $args Additional arguments
|
*
|
* @return array HTML attributes for tracking
|
*/
|
public function trackContentClick(int $id, string $type, array $args = []):array
|
{
|
$args['id'] = $id;
|
|
// Auto-detect owner for content types
|
if (empty($args['owner_id']) && in_array($type, [ 'jvb_tattoo', 'jvb_artwork', 'jvb_piercing' ])) {
|
$post = get_post($id);
|
if ($post && ! empty($post->post_author)) {
|
$args['owner_id'] = $post->post_author;
|
$args['owner_type'] = 'user';
|
}
|
}
|
|
return $this->buildAttributes('click_content', $type, $args);
|
}
|
|
/**
|
* Build tracking attributes for profile clicks
|
*
|
* @param int $id Profile ID
|
* @param string $type Profile type (usually jvb_artist or jvb_partner)
|
* @param array $args Additional arguments
|
*
|
* @return array HTML attributes for tracking
|
*/
|
public function trackProfileClick(int $id, string $type, array $args = []):array
|
{
|
$args['id'] = $id;
|
return $this->buildAttributes('click_profile', $type, $args);
|
}
|
|
/**
|
* Build tracking attributes for taxonomy clicks
|
*
|
* @param int $id Term ID
|
* @param string $taxonomy Taxonomy
|
* @param array $args Additional arguments
|
*
|
* @return array HTML attributes for tracking
|
*/
|
public function trackTaxonomyClick(int $id, string $taxonomy, array $args = [])
|
{
|
$args['id'] = $id;
|
return $this->buildAttributes('click_taxonomy', $taxonomy, $args);
|
}
|
|
/**
|
* Build tracking attributes for shop clicks
|
*
|
* @param int $id Shop ID
|
* @param array $args Additional arguments
|
*
|
* @return array HTML attributes for tracking
|
*/
|
public function trackShopClick(int $id, string $type = 'shop', array $args = []):array
|
{
|
$args['id'] = $id;
|
return $this->buildAttributes('click_shop', $type, $args);
|
}
|
|
/**
|
* Build tracking attributes for favourite toggles
|
*
|
* @param int $id Content ID
|
* @param string $type Content type
|
* @param bool $is_favourite Whether it's being favourited or unfavourited
|
* @param array $args Additional arguments
|
*
|
* @return array HTML attributes for tracking
|
*/
|
public function trackFavouriteToggle(int $id, string $type, bool $is_favourite, array $args = [])
|
{
|
$args['id'] = $id;
|
$args['meta']['action'] = $is_favourite ? 'add' : 'remove';
|
return $this->buildAttributes('toggle_favourite', $type, $args);
|
}
|
|
/**
|
* Track content view in feed
|
*
|
* @param int $id Content ID
|
* @param string $type Content type
|
* @param array $args Additional arguments
|
*
|
* @return array HTML attributes for tracking
|
*/
|
public function trackFeedView(int $id, string $type, array $args = []):array
|
{
|
$args['id'] = $id;
|
return $this->buildAttributes('view_feed', $type, $args);
|
}
|
|
/**
|
* Convert an array of attributes to a string
|
*
|
* @param array $attributes Attributes array
|
*
|
* @return string Attributes string
|
*/
|
|
public function attributesToString(array $attributes):string
|
{
|
$attr_strings = [];
|
|
foreach ($attributes as $key => $value) {
|
$attr_strings[] = $key . '="' . $value . '"';
|
}
|
|
return implode(' ', $attr_strings);
|
}
|
}
|