Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ const jsDefaults = {

/** All build targets. */
const builds = [
// --- Frontend JS (main guide) ---
// --- Frontend JS (main guide) — bundled from ES modules ---
{
...jsDefaults,
bundle: true,
entryPoints: ['frontend/js/rivian-tires.js'],
outfile: 'frontend/js/rivian-tires.min.js',
format: 'iife',
minify: true,
drop: ['console', 'debugger'],
},
Expand Down
65 changes: 65 additions & 0 deletions frontend/js/modules/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* jshint esversion: 11 */

/**
* Analytics tracking module.
*/

// --- Analytics Tracking ---
export const RTG_ANALYTICS = {
nonce: null,
ajaxurl: null,

init() {
if (typeof rtgData !== 'undefined' && rtgData.settings) {
this.nonce = rtgData.settings.analyticsNonce || null;
this.ajaxurl = rtgData.settings.ajaxurl || null;
}
},

/**
* Send a tracking beacon. Uses sendBeacon primary, fetch+keepalive fallback.
*/
track(action, data) {
if (!this.ajaxurl || !this.nonce) return;

const payload = new FormData();
payload.append('action', action);
payload.append('nonce', this.nonce);
Object.entries(data).forEach(([k, v]) => payload.append(k, v));

if (navigator.sendBeacon) {
const sent = navigator.sendBeacon(this.ajaxurl, payload);
if (sent) return;
}

try {
fetch(this.ajaxurl, {
method: 'POST',
body: payload,
keepalive: true,
}).catch(() => {});
} catch (e) {
// Analytics should never break the page.
}
},

trackClick(tireId, linkType) {
this.track('rtg_track_click', {
tire_id: tireId,
link_type: linkType,
});
},

_searchDebounceTimer: null,
trackSearch(query, filters, sortBy, resultCount) {
clearTimeout(this._searchDebounceTimer);
this._searchDebounceTimer = setTimeout(() => {
this.track('rtg_track_search', {
search_query: query,
filters_json: JSON.stringify(filters),
sort_by: sortBy,
result_count: String(resultCount),
});
}, 2000);
},
};
Loading