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'; 'use strict';
const params = wc_order_source_attribution.params;
const prefix = params.prefix; const prefix = params.prefix;
const cookieLifetime = Number( params.lifetime ); const cookieLifetime = Number( params.lifetime );
const sessionLength = Number( params.session ); const sessionLength = Number( params.session );
window.woocommerce_order_source_attribution = {};
woocommerce_order_source_attribution.initOrderTracking = () => { wc_order_source_attribution.initOrderTracking = () => {
if ( params.allowTracking === 'no' ) { if ( params.allowTracking === 'no' ) {
woocommerce_order_source_attribution.removeTrackingCookies(); wc_order_source_attribution.removeTrackingCookies();
return; return;
} }
@ -66,19 +66,19 @@
/** /**
* Enable or disable order tracking analytics and marketing consent init and change. * 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 ) { if ( ! allow ) {
return; return;
} }
params.allowTracking = 'yes'; 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. * 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 domain = window.location.hostname;
const sbCookies = [ const sbCookies = [
'sbjs_current', 'sbjs_current',
@ -99,6 +99,6 @@
} }
// Run init. // 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 ) { window.addEventListener( 'load' , function ( e ) {
if ( window.wccom && window.wccom.canTrackUser('analytics') ) { 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 ) { for ( const key in changedConsentCategory ) {
if ( changedConsentCategory.hasOwnProperty( key ) ) { if ( changedConsentCategory.hasOwnProperty( key ) ) {
if ( key === CONSENT_CATEGORY_MARKING && changedConsentCategory[ key ] === 'allow' ) { 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. // Init order source attribution as soon as consent type is defined.
$( document ).on( 'wp_consent_type_defined', () => { $( document ).on( 'wp_consent_type_defined', () => {
if ( wp_has_consent( CONSENT_CATEGORY_MARKING ) ) { if ( wp_has_consent( CONSENT_CATEGORY_MARKING ) ) {
window.woocommerce_order_source_attribution.setAllowTrackingConsent( true ); window.wc_order_source_attribution.setAllowTrackingConsent( true );
} }
} ); } );
}( jQuery ) ); }( 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 ) ); $allow_tracking = wc_bool_to_string( apply_filters( 'wc_order_source_attribution_allow_tracking', true ) );
// Pass parameters to Order Source Attribution JS. // Create Order Source Attribution JS namespace with parameters.
$params = array( $namespace = array(
'params' => array (
'lifetime' => $lifetime, 'lifetime' => $lifetime,
'session' => $session_length, 'session' => $session_length,
'ajaxurl' => admin_url( 'admin-ajax.php' ), 'ajaxurl' => admin_url( 'admin-ajax.php' ),
'prefix' => $this->field_prefix, 'prefix' => $this->field_prefix,
'allowTracking' => $allow_tracking, '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 );
} }
/** /**