Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
96 changes: 96 additions & 0 deletions extensions/kilkaya/k5a_meta_send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* Post Loader — Send Kilkaya conversion tracking */
/* global a, b */
/* eslint-disable-next-line no-unused-vars */
(function (a, b) {
try {
if (String(b.event_name) !== 'checkout' || String(b.event_action) !== 'success') {
return;
}

// Helper to log to localStorage (survives redirect)
var persistLog = function(message, data) {
try {
var log = {
timestamp: new Date().toISOString(),
message: message,
data: data
};
localStorage.setItem('k5a_send_log', JSON.stringify(log));
console.log('[K5A SEND] ' + message, data);

Check warning on line 19 in extensions/kilkaya/k5a_meta_send.js

View workflow job for this annotation

GitHub Actions / Run Unit Tests

Unexpected console statement
} catch (e) {
console.log('[K5A SEND] ' + message, data);

Check warning on line 21 in extensions/kilkaya/k5a_meta_send.js

View workflow job for this annotation

GitHub Actions / Run Unit Tests

Unexpected console statement
}
};

persistLog('Checkout success detected', {k5aMeta: window.k5aMeta});

// Wait for k5aMeta.conversion to be set by conversion extension
setTimeout(function() {
try {
// Build the tracking URL manually based on Kilkaya's format
var installationId = '68ee5be64709bd7f4b3e3bf2';
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The installation ID is hardcoded. Consider making this configurable through window.k5aMeta or a configuration object to support different installations without code changes.

Suggested change
var installationId = '68ee5be64709bd7f4b3e3bf2';
var installationId = (window.k5aMeta && window.k5aMeta.installationId) ? window.k5aMeta.installationId : '68ee5be64709bd7f4b3e3bf2';

Copilot uses AI. Check for mistakes.
var baseUrl = 'https://cl-eu10.k5a.io/';

// Get page data
var pageData = window.k5aMeta || {};

// Build query parameters for Kilkaya
var params = [];
params.push('i=' + encodeURIComponent(installationId));
params.push('l=p'); // pageview log type
params.push('cs=1'); // conversion status = 1
params.push('_s=conversion');
params.push('_m=b'); // method=beacon


// Add conversion-specific data
if (pageData.conversion) params.push('cv=' + pageData.conversion);
if (pageData.cntTag && Array.isArray(pageData.cntTag) && pageData.cntTag.length > 0) {
params.push('cntt=' + encodeURIComponent(pageData.cntTag.join(',')));
}

var trackingUrl = baseUrl + '?' + params.join('&');

persistLog('Tracking URL built', {url: trackingUrl});

// Try sendBeacon first (best for page unloads)
if (navigator.sendBeacon) {
var sent = navigator.sendBeacon(trackingUrl);

if (sent) {
persistLog('✓ SUCCESS: Sent via sendBeacon', {
url: trackingUrl,
method: 'sendBeacon'
});
return;
}
}

// Fallback: try kilkaya API if available
if (window.kilkaya && window.kilkaya.logger &&
typeof window.kilkaya.logger.fireNow === 'function') {

var logData = window.kilkaya.pageData.getDefaultData();
logData.cs = 1; // conversion
window.kilkaya.logger.fireNow('pageView', logData, 'conversion');
persistLog('✓ SUCCESS: Sent via Kilkaya API', {method: 'kilkaya.logger.fireNow'});
return;
}

} catch (err) {
persistLog('✗ ERROR sending conversion', {error: err.message, stack: err.stack});
}
}, 150); // Small delay to ensure k5aMeta.conversion is set
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 150ms delay is a magic number with unclear justification. Consider extracting this to a named constant (e.g., CONVERSION_DATA_WAIT_MS) to improve code clarity and maintainability.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback


} catch (e) {
try {
localStorage.setItem('k5a_send_log', JSON.stringify({
timestamp: new Date().toISOString(),
message: '✗ CRITICAL ERROR',
data: {error: e.message, stack: e.stack}
}));
} catch (storageErr) {
console.error('[K5A SEND] Error:', e);

Check warning on line 93 in extensions/kilkaya/k5a_meta_send.js

View workflow job for this annotation

GitHub Actions / Run Unit Tests

Unexpected console statement
}
}
})(a, b);
Loading
Loading