WC_Site_Tracking::add_tracking_function()public staticWC 1.0

Adds the tracking function to the admin footer.

Method of the class: WC_Site_Tracking{}

Hooks from the method

Return

null. Nothing (null).

Usage

$result = WC_Site_Tracking::add_tracking_function();

WC_Site_Tracking::add_tracking_function() code WC 9.3.3

<?php
public static function add_tracking_function() {
	$user            = wp_get_current_user();
	$server_details  = WC_Tracks::get_server_details();
	$blog_details    = WC_Tracks::get_blog_details( $user->ID );
	$tracks_identity = WC_Tracks_Client::get_identity( $user->ID );

	$client_tracking_properties = array_merge( $server_details, $blog_details );
	/**
	 * Add global tracks event properties.
	 *
	 * @since 6.5.0
	 */
	$filtered_properties = apply_filters( 'woocommerce_tracks_event_properties', $client_tracking_properties, false );
	$environment_type    = function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production';
	?>
	<!-- WooCommerce Tracks -->
	<script type="text/javascript">
		window.wcTracks = window.wcTracks || {};
		window.wcTracks.isEnabled = <?php echo self::is_tracking_enabled() ? 'true' : 'false'; ?>;
		window._tkq = window._tkq || [];

		<?php if ( 'anon' !== $tracks_identity['_ut'] ) { ?>
		window._tkq.push( [ 'identifyUser', '<?php echo esc_js( $tracks_identity['_ui'] ); ?>' ] );
		<?php } ?>
		window.wcTracks.validateEvent = function( eventName, props = {} ) {
			let isValid = true;
			if ( ! <?php echo esc_js( WC_Tracks_Event::EVENT_NAME_REGEX ); ?>.test( eventName ) ) {
				if ( <?php echo $environment_type !== 'production' ? 'true' : 'false'; ?> ) {
					/* eslint-disable no-console */
					console.error(
						`A valid event name must be specified. The event name: "${ eventName }" is not valid.`
					);
					/* eslint-enable no-console */
				}
				isValid = false;
			}
			for ( const prop of Object.keys( props ) ) {
				if ( ! <?php echo esc_js( WC_Tracks_Event::PROP_NAME_REGEX ); ?>.test( prop ) ) {
					if ( <?php echo $environment_type !== 'production' ? 'true' : 'false'; ?> ) {
						/* eslint-disable no-console */
						console.error(
							`A valid prop name must be specified. The property name: "${ prop }" is not valid.`
						);
						/* eslint-enable no-console */
					}
					isValid = false;
				}
			}
			return isValid;
		}
		window.wcTracks.recordEvent = function( name, properties ) {
			if ( ! window.wcTracks.isEnabled ) {
				return;
			}

			const eventName = '<?php echo esc_attr( WC_Tracks::PREFIX ); ?>' + name;
			let eventProperties = properties || {};
			eventProperties = { ...eventProperties, ...<?php echo json_encode( $filtered_properties ); ?> };
			if ( window.wp && window.wp.hooks && window.wp.hooks.applyFilters ) {
				eventProperties = window.wp.hooks.applyFilters( 'woocommerce_tracks_client_event_properties', eventProperties, eventName );
				delete( eventProperties._ui );
				delete( eventProperties._ut );
			}

			if ( ! window.wcTracks.validateEvent( eventName, eventProperties ) ) {
				return;
			}
			window._tkq.push( [ 'recordEvent', eventName, eventProperties ] );
		}
	</script>
	<?php
}