plugin updates
This commit is contained in:
@@ -5,93 +5,78 @@
|
||||
|
||||
// Helper functions.
|
||||
const $ = document.querySelector.bind( document );
|
||||
const propertyAccessor = ( obj, path ) => path.split( '.' ).reduce( ( acc, part ) => acc && acc[ part ], obj );
|
||||
const returnNull = () => null;
|
||||
|
||||
// Hardcode Checkout store key (`wc.wcBlocksData.CHECKOUT_STORE_KEY`), as we no longer have `wc-blocks-checkout` as a dependency.
|
||||
const CHECKOUT_STORE_KEY = 'wc/store/checkout';
|
||||
|
||||
/**
|
||||
* Flattens the sbjs.get object into a schema compatible object.
|
||||
* Get the order attribution data.
|
||||
*
|
||||
* @param {Object} obj Sourcebuster data object, `sbjs.get`.
|
||||
* @returns
|
||||
* Returns object full of `null`s if tracking is disabled.
|
||||
*
|
||||
* @returns {Object} Schema compatible object.
|
||||
*/
|
||||
wc_order_attribution.sbjsDataToSchema = ( obj ) => ( {
|
||||
type: obj.current.typ,
|
||||
url: obj.current_add.rf,
|
||||
|
||||
utm_campaign: obj.current.cmp,
|
||||
utm_source: obj.current.src,
|
||||
utm_medium: obj.current.mdm,
|
||||
utm_content: obj.current.cnt,
|
||||
utm_id: obj.current.id,
|
||||
utm_term: obj.current.trm,
|
||||
|
||||
session_entry: obj.current_add.ep,
|
||||
session_start_time: obj.current_add.fd,
|
||||
session_pages: obj.session.pgs,
|
||||
session_count: obj.udata.vst,
|
||||
|
||||
user_agent: obj.udata.uag,
|
||||
} );
|
||||
function getData() {
|
||||
const accessor = params.allowTracking ? propertyAccessor : returnNull;
|
||||
const entries = Object.entries( wc_order_attribution.fields )
|
||||
.map( ( [ key, property ] ) => [ key, accessor( sbjs.get, property ) ] );
|
||||
return Object.fromEntries( entries );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the module.
|
||||
* Update `wc_order_attribution` input elements' values.
|
||||
*
|
||||
* @param {Object} values Object containing field values.
|
||||
*/
|
||||
function initOrderTracking() {
|
||||
if ( params.allowTracking === 'no' ) {
|
||||
removeTrackingCookies();
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize sourcebuster.js.
|
||||
*/
|
||||
sbjs.init( {
|
||||
lifetime: Number( params.lifetime ),
|
||||
session_length: Number( params.session ),
|
||||
timezone_offset: '0', // utc
|
||||
} );
|
||||
|
||||
/**
|
||||
* Callback to set visitor source values in the checkout
|
||||
* and register forms using sourcebuster object values.
|
||||
* More info at https://sbjs.rocks/#/usage.
|
||||
*/
|
||||
const setFields = () => {
|
||||
|
||||
if ( sbjs.get ) {
|
||||
for( const [ key, value ] of Object.entries( wc_order_attribution.sbjsDataToSchema( sbjs.get ) ) ) {
|
||||
$( `input[name="${params.prefix}${key}"]` ).value = value;
|
||||
}
|
||||
function updateFormValues( values ) {
|
||||
// Update inputs if any exist.
|
||||
if( $( `input[name^="${params.prefix}"]` ) ) {
|
||||
for( const key of Object.keys( wc_order_attribution.fields ) ) {
|
||||
$( `input[name="${params.prefix}${key}"]` ).value = values && values[ key ] || '';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Add source values to the classic checkout form.
|
||||
*/
|
||||
if ( $( 'form.woocommerce-checkout' ) !== null ) {
|
||||
const previousInitCheckout = document.body.oninit_checkout;
|
||||
document.body.oninit_checkout = () => {
|
||||
setFields();
|
||||
previousInitCheckout && previousInitCheckout();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Add source values to register form.
|
||||
*/
|
||||
if ( $( '.woocommerce form.register' ) !== null ) {
|
||||
setFields();
|
||||
};
|
||||
|
||||
/**
|
||||
* Update Checkout extension data.
|
||||
*
|
||||
* @param {Object} values Object containing field values.
|
||||
*/
|
||||
function updateCheckoutBlockData( values ) {
|
||||
// Update Checkout block data if available.
|
||||
if ( window.wp && window.wp.data && window.wp.data.dispatch && window.wc && window.wc.wcBlocksData ) {
|
||||
window.wp.data.dispatch( window.wc.wcBlocksData.CHECKOUT_STORE_KEY ).__internalSetExtensionData(
|
||||
'woocommerce/order-attribution',
|
||||
values,
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable order tracking analytics and marketing consent init and change.
|
||||
* Initialize sourcebuster & set data, or clear cookies & data.
|
||||
*
|
||||
* @param {boolean} allow Whether to allow tracking or disable it.
|
||||
*/
|
||||
wc_order_attribution.setAllowTrackingConsent = ( allow ) => {
|
||||
wc_order_attribution.setOrderTracking = function( allow ) {
|
||||
params.allowTracking = allow;
|
||||
if ( ! allow ) {
|
||||
return;
|
||||
// Reset cookies, and clear form data.
|
||||
removeTrackingCookies();
|
||||
} else {
|
||||
// If not done yet, initialize sourcebuster.js which populates `sbjs.get` object.
|
||||
sbjs.init( {
|
||||
lifetime: Number( params.lifetime ),
|
||||
session_length: Number( params.session ),
|
||||
timezone_offset: '0', // utc
|
||||
} );
|
||||
}
|
||||
|
||||
params.allowTracking = 'yes';
|
||||
initOrderTracking();
|
||||
const values = getData();
|
||||
updateFormValues( values );
|
||||
updateCheckoutBlockData( values );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,6 +103,37 @@
|
||||
}
|
||||
|
||||
// Run init.
|
||||
initOrderTracking();
|
||||
wc_order_attribution.setOrderTracking( params.allowTracking );
|
||||
|
||||
// Wait for (async) classic checkout initialization and set source values once loaded.
|
||||
if ( $( 'form.woocommerce-checkout' ) !== null ) {
|
||||
const previousInitCheckout = document.body.oninit_checkout;
|
||||
document.body.oninit_checkout = () => {
|
||||
updateFormValues( getData() );
|
||||
previousInitCheckout && previousInitCheckout();
|
||||
};
|
||||
}
|
||||
|
||||
// Work around the lack of explicit script dependency for the checkout block.
|
||||
// Conditionally, wait for and use 'wp-data' & 'wc-blocks-checkout.
|
||||
|
||||
// Wait for (async) block checkout initialization and set source values once loaded.
|
||||
function eventuallyInitializeCheckoutBlock() {
|
||||
if (
|
||||
window.wp && window.wp.data && typeof window.wp.data.subscribe === 'function'
|
||||
) {
|
||||
// Update checkout block data once more if the checkout store was loaded after this script.
|
||||
const unsubscribe = window.wp.data.subscribe( function () {
|
||||
unsubscribe();
|
||||
updateCheckoutBlockData( getData() );
|
||||
}, CHECKOUT_STORE_KEY );
|
||||
}
|
||||
};
|
||||
// Wait for DOMContentLoaded to make sure wp.data is in place, if applicable for the page.
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", eventuallyInitializeCheckoutBlock);
|
||||
} else {
|
||||
eventuallyInitializeCheckoutBlock();
|
||||
}
|
||||
|
||||
}( window.wc_order_attribution ) );
|
||||
|
||||
Reference in New Issue
Block a user