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
11 changes: 3 additions & 8 deletions extensions/kilkaya/k5a_meta_conversion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Pre Loader — k5aMeta conversion for checkout success */
/* global utag, a, b */
/* global a, b */
/* eslint-disable-next-line no-unused-vars */
(function (a, b) {
try {
Expand All @@ -24,13 +24,8 @@
window.k5aMeta.cntTag.push('offer_' + String(b.offer_id));
}

if (window.utag && window.utag.cfg && window.utag.cfg.utDebug) {
utag.DB('k5aMeta conversion set for checkout success');
}

} catch (e) {
if (window.utag && window.utag.cfg && window.utag.cfg.utDebug) {
utag.DB('k5aMeta conversion error: ' + e);
}
// Silent error handling - conversion tracking should not break page functionality
console.error('[K5A CONVERSION] Error:', e);

Check warning on line 29 in extensions/kilkaya/k5a_meta_conversion.js

View workflow job for this annotation

GitHub Actions / Run Unit Tests

Unexpected console statement
}
})(a, b);
111 changes: 111 additions & 0 deletions extensions/kilkaya/k5a_meta_send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* 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 and utag data
var pageData = window.k5aMeta || {};
var U = (window.utag && window.utag.data) || {};

// 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('nopv=1'); // Don't log as pageview, only sale
params.push('_s=conversion');
params.push('_m=b'); // method=beacon

// REQUIRED: Add URL parameter (u=)
var url = pageData.url || U['dom.url'] || document.URL;
if (url) {
params.push('u=' + encodeURIComponent(url));
}

// Add channel/platform (c=desktop|mobile)
var platform = U.page_platform || U['cp.utag_main_page_platform'] || '';
if (platform) {
// Normalize platform value to desktop or mobile
var channel = (platform.toLowerCase() === 'mobile') ? 'mobile' : 'desktop';
params.push('c=' + encodeURIComponent(channel));
}

// 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 108 in extensions/kilkaya/k5a_meta_send.js

View workflow job for this annotation

GitHub Actions / Run Unit Tests

Unexpected console statement
}
}
})(a, b);
83 changes: 9 additions & 74 deletions tests/kilkaya/k5a_meta_conversion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ describe('k5a_meta_conversion', () => {
// Save original utag if it exists
originalUtag = window.utag;

// Mock utag
window.utag = {
data: {},
cfg: {
utDebug: true
},
DB: jest.fn()
};
// Mock console.error (since we removed utag.DB)
jest.spyOn(console, 'error').mockImplementation();

// Clean up global variables
delete global.a;
Expand All @@ -36,6 +30,7 @@ describe('k5a_meta_conversion', () => {
}
delete global.a;
delete global.b;
jest.restoreAllMocks();
jest.resetModules();
});

Expand All @@ -53,7 +48,6 @@ describe('k5a_meta_conversion', () => {
expect(window.k5aMeta.conversion).toBe(1);
expect(Array.isArray(window.k5aMeta.cntTag)).toBe(true);
expect(window.k5aMeta.cntTag).toContain('offer_12345');
expect(window.utag.DB).toHaveBeenCalledWith('k5aMeta conversion set for checkout success');
});

it('should not set conversion if event_name is not "checkout"', () => {
Expand All @@ -67,7 +61,6 @@ describe('k5a_meta_conversion', () => {
require('../../extensions/kilkaya/k5a_meta_conversion.js');

expect(window.k5aMeta).toBeUndefined();
expect(window.utag.DB).not.toHaveBeenCalledWith('k5aMeta conversion set for checkout success');
});

it('should not set conversion if event_action is not "success"', () => {
Expand All @@ -81,7 +74,6 @@ describe('k5a_meta_conversion', () => {
require('../../extensions/kilkaya/k5a_meta_conversion.js');

expect(window.k5aMeta).toBeUndefined();
expect(window.utag.DB).not.toHaveBeenCalledWith('k5aMeta conversion set for checkout success');
});

it('should initialize k5aMeta object if it does not exist', () => {
Expand Down Expand Up @@ -235,13 +227,16 @@ describe('k5a_meta_conversion', () => {

require('../../extensions/kilkaya/k5a_meta_conversion.js');

expect(window.utag.DB).toHaveBeenCalledWith(expect.stringContaining('k5aMeta conversion error:'));
expect(console.error).toHaveBeenCalledWith(
expect.stringContaining('[K5A CONVERSION] Error:'),
expect.any(Error)
);

// Clean up the read-only property
delete window.k5aMeta;
});

it('should handle missing utag gracefully', () => {
it('should work without utag dependency', () => {
global.a = 'some_value';
global.b = {
event_name: 'checkout',
Expand All @@ -250,9 +245,8 @@ describe('k5a_meta_conversion', () => {
};

delete window.utag;
global.utag = undefined;

// With the guard in place, the code should not throw even if utag is missing
// Should work fine without utag
expect(() => {
require('../../extensions/kilkaya/k5a_meta_conversion.js');
}).not.toThrow();
Expand All @@ -262,25 +256,6 @@ describe('k5a_meta_conversion', () => {
expect(window.k5aMeta.conversion).toBe(1);
expect(window.k5aMeta.cntTag).toContain('offer_12345');
});
it('should work when utag is defined but utag.data is missing', () => {
global.a = 'some_value';
global.b = {
event_name: 'checkout',
event_action: 'success',
offer_id: '12345'
};

window.utag = {
DB: jest.fn()
};
// utag.data is undefined

require('../../extensions/kilkaya/k5a_meta_conversion.js');

expect(window.k5aMeta).toBeDefined();
expect(window.k5aMeta.conversion).toBe(1);
expect(window.k5aMeta.cntTag).toContain('offer_12345');
});

it('should handle event_name and event_action type coercion', () => {
global.a = 'some_value';
Expand Down Expand Up @@ -323,44 +298,4 @@ describe('k5a_meta_conversion', () => {
expect(window.k5aMeta.cntTag).toContain('offer_special-offer_123@test');
});

it('should not log when debug mode is disabled', () => {
global.a = 'some_value';
global.b = {
event_name: 'checkout',
event_action: 'success',
offer_id: '12345'
};

window.utag.cfg.utDebug = false;

require('../../extensions/kilkaya/k5a_meta_conversion.js');

expect(window.k5aMeta.conversion).toBe(1);
expect(window.utag.DB).not.toHaveBeenCalled();
});

it('should not log errors when debug mode is disabled', () => {
global.a = 'some_value';
global.b = {
event_name: 'checkout',
event_action: 'success',
offer_id: '12345'
};

window.utag.cfg.utDebug = false;

// Force an error
Object.defineProperty(window, 'k5aMeta', {
value: null,
writable: false,
configurable: true
});

require('../../extensions/kilkaya/k5a_meta_conversion.js');

expect(window.utag.DB).not.toHaveBeenCalled();

// Clean up
delete window.k5aMeta;
});
});
Loading
Loading