Jake Vanderwerf
2026-01-05 9f86429a1252b45c95b7c62fbaa1b82de3723997
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?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);
    }
}