Skip to content

Commit

Permalink
Reduce wc_order_attribution module API
Browse files Browse the repository at this point in the history
make `initOrderTracking` and `removeTrackingCookies` methods private,as per YAGNI.
Add some more code comments
  • Loading branch information
tomalec authored and layoutd committed Nov 27, 2023
1 parent 73dd781 commit 19bb2a3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Extend checkout block's request data with order attribution data.
window.wp.data.dispatch( window.wc.wcBlocksData.CHECKOUT_STORE_KEY ).__internalSetExtensionData(
'woocommerce/order-attribution',
window.wc_order_attribution.sbjsDataToSchema( window.sbjs.get )
Expand Down
32 changes: 16 additions & 16 deletions plugins/woocommerce/client/legacy/js/frontend/order-attribution.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
( function ( wc_order_attribution ) {
'use strict';

// Cache params reference for shorter reusability.
const params = wc_order_attribution.params;
const prefix = params.prefix;
const cookieLifetime = Number( params.lifetime );
const sessionLength = Number( params.session );

// Helper functions.
const $ = document.querySelector.bind( document );

/**
Expand Down Expand Up @@ -33,19 +31,21 @@
user_agent: obj.udata.uag,
} );

wc_order_attribution.initOrderTracking = () => {

/**
* Initialize the module.
*/
function initOrderTracking() {
if ( params.allowTracking === 'no' ) {
wc_order_attribution.removeTrackingCookies();
removeTrackingCookies();
return;
}

/**
* Initialize sourcebuster.js.
*/
sbjs.init( {
lifetime: cookieLifetime,
session_length: sessionLength,
lifetime: Number( params.lifetime ),
session_length: Number( params.session ),
timezone_offset: '0', // utc
} );

Expand All @@ -64,7 +64,7 @@
};

/**
* Add source values to the classic checkout.
* Add source values to the classic checkout form.
*/
if ( $( 'form.woocommerce-checkout' ) !== null ) {
const previousInitCheckout = document.body.oninit_checkout;
Expand All @@ -75,7 +75,7 @@
}

/**
* Add source values to register.
* Add source values to register form.
*/
if ( $( '.woocommerce form.register' ) !== null ) {
setFields();
Expand All @@ -91,13 +91,14 @@
}

params.allowTracking = 'yes';
wc_order_attribution.initOrderTracking();
initOrderTracking();
}

/**
* Remove sourcebuster.js cookies whenever tracking is disabled or consent is revoked.
* Remove sourcebuster.js cookies.
* To be called whenever tracking is disabled or consent is revoked.
*/
wc_order_attribution.removeTrackingCookies = () => {
function removeTrackingCookies() {
const domain = window.location.hostname;
const sbCookies = [
'sbjs_current',
Expand All @@ -114,10 +115,9 @@
sbCookies.forEach( ( name ) => {
document.cookie = `${name}=; path=/; max-age=-999; domain=.${domain};`;
} );

}

// Run init.
wc_order_attribution.initOrderTracking();
initOrderTracking();

}( window.wc_order_attribution ) );

0 comments on commit 19bb2a3

Please sign in to comment.