Use single public namespace for OSA JS

This commit is contained in:
Tomek Wytrębowicz 2023-11-20 15:37:08 +01:00 committed by Justin Palmer
parent ceb354e456
commit 52318b4de7
No known key found for this signature in database
GPG Key ID: ACAB7C35AA2577AF
4 changed files with 22 additions and 20 deletions

View File

@ -1,16 +1,16 @@
( function ( $, params ) {
( function ( $, wc_order_source_attribution ) {
'use strict';
const params = wc_order_source_attribution.params;
const prefix = params.prefix;
const cookieLifetime = Number( params.lifetime );
const sessionLength = Number( params.session );
window.woocommerce_order_source_attribution = {};
woocommerce_order_source_attribution.initOrderTracking = () => {
wc_order_source_attribution.initOrderTracking = () => {
if ( params.allowTracking === 'no' ) {
woocommerce_order_source_attribution.removeTrackingCookies();
wc_order_source_attribution.removeTrackingCookies();
return;
}
@ -66,19 +66,19 @@
/**
* Enable or disable order tracking analytics and marketing consent init and change.
*/
woocommerce_order_source_attribution.setAllowTrackingConsent = ( allow ) => {
wc_order_source_attribution.setAllowTrackingConsent = ( allow ) => {
if ( ! allow ) {
return;
}
params.allowTracking = 'yes';
woocommerce_order_source_attribution.initOrderTracking();
wc_order_source_attribution.initOrderTracking();
}
/**
* Remove sourcebuster.js cookies whenever tracking is disabled or consent is revoked.
*/
woocommerce_order_source_attribution.removeTrackingCookies = () => {
wc_order_source_attribution.removeTrackingCookies = () => {
const domain = window.location.hostname;
const sbCookies = [
'sbjs_current',
@ -99,6 +99,6 @@
}
// Run init.
woocommerce_order_source_attribution.initOrderTracking();
wc_order_source_attribution.initOrderTracking();
}( jQuery, window.wc_order_attribute_source_params ) );
}( jQuery, window.wc_order_source_attribution ) );

View File

@ -1,6 +1,6 @@
window.addEventListener( 'load' , function ( e ) {
if ( window.wccom && window.wccom.canTrackUser('analytics') ) {
window.woocommerce_order_source_attribution.setAllowTrackingConsent( true );
window.wc_order_source_attribution.setAllowTrackingConsent( true );
}
} );

View File

@ -8,7 +8,7 @@
for ( const key in changedConsentCategory ) {
if ( changedConsentCategory.hasOwnProperty( key ) ) {
if ( key === CONSENT_CATEGORY_MARKING && changedConsentCategory[ key ] === 'allow' ) {
window.woocommerce_order_source_attribution.setAllowTrackingConsent( true );
window.wc_order_source_attribution.setAllowTrackingConsent( true );
}
}
}
@ -17,7 +17,7 @@
// Init order source attribution as soon as consent type is defined.
$( document ).on( 'wp_consent_type_defined', () => {
if ( wp_has_consent( CONSENT_CATEGORY_MARKING ) ) {
window.woocommerce_order_source_attribution.setAllowTrackingConsent( true );
window.wc_order_source_attribution.setAllowTrackingConsent( true );
}
} );
}( jQuery ) );

View File

@ -221,16 +221,18 @@ class SourceAttributionController implements RegisterHooksInterface {
*/
$allow_tracking = wc_bool_to_string( apply_filters( 'wc_order_source_attribution_allow_tracking', true ) );
// Pass parameters to Order Source Attribution JS.
$params = array(
'lifetime' => $lifetime,
'session' => $session_length,
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'prefix' => $this->field_prefix,
'allowTracking' => $allow_tracking,
// Create Order Source Attribution JS namespace with parameters.
$namespace = array(
'params' => array (
'lifetime' => $lifetime,
'session' => $session_length,
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'prefix' => $this->field_prefix,
'allowTracking' => $allow_tracking,
),
);
wp_localize_script( 'woocommerce-order-source-attribution-js', 'wc_order_attribute_source_params', $params );
wp_localize_script( 'woocommerce-order-source-attribution-js', 'wc_order_source_attribution', $namespace );
}
/**