<?php $get_hash = $this->get_localstorage_hash( 'fb' ); ?>
var wfocuFBHash = '<?php echo $get_hash; ?>';
var wfocuFBResult = false;
try {

if ('1' === localStorage.getItem("wfocuH_" + wfocuFBHash)) {
wfocuFBResult = true;
}

} catch (exception) {

}

if(false === wfocuFBResult) {
<?php
if ( $this->do_track_fb_synced_purchase() && is_array( $data ) && isset( $data['fb'] ) ) {
	$event_id = $this->get_event_id( 'Purchase' );
	?>
	var wfocufbDataPurchaseTrackSynced = {
	contents: <?php echo wp_json_encode( array_values( $data['fb']['products'] ) ); ?>,
	content_type: 'product',
	content_ids: <?php echo wp_json_encode( $data['fb']['content_ids'] ); ?>,
	value: <?php echo esc_js( $data['fb']['total'] ); ?>,
	currency: '<?php echo esc_js( $data['fb']['currency'] ); ?>',
	content_name: <?php echo wp_json_encode( implode( ',', $data['fb']['content_name'] ) ); ?>,
	category_name: <?php echo wp_json_encode( implode( ',', $data['fb']['category_name'] ) ); ?>,
	num_items: <?php echo esc_js( $data['fb']['num_qty'] ); ?>,
	transaction_id: '<?php echo esc_js( $data['fb']['transaction_id'] ); ?>',
    'order_id': '<?php echo esc_js( $data['fb']['order_id'] ); ?>'
	};

	<?php if ( isset( $data['fb']['additional']['town'] ) && ! empty( $data['fb']['additional']['town'] ) ) { ?>
		wfocufbDataPurchaseTrackSynced.town = '<?php echo esc_js( $data['fb']['additional']['town'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['additional']['country'] ) && ! empty( $data['fb']['additional']['country'] ) ) { ?>
		wfocufbDataPurchaseTrackSynced.country = '<?php echo esc_js( $data['fb']['additional']['country'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['additional']['state'] ) && ! empty( $data['fb']['additional']['state'] ) ) { ?>
		wfocufbDataPurchaseTrackSynced.state = '<?php echo esc_js( $data['fb']['additional']['state'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['additional']['payment'] ) && ! empty( $data['fb']['additional']['payment'] ) ) { ?>
		wfocufbDataPurchaseTrackSynced.payment = '<?php echo esc_js( $data['fb']['additional']['payment'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['additional']['shipping'] ) && ! empty( $data['fb']['additional']['shipping'] ) ) { ?>
		wfocufbDataPurchaseTrackSynced.shipping = '<?php echo esc_js( $data['fb']['additional']['shipping'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['additional']['coupon_name'] ) && ! empty( $data['fb']['additional']['coupon_name'] ) ) { ?>
		wfocufbDataPurchaseTrackSynced.coupon_name = '<?php echo esc_js( $data['fb']['additional']['coupon_name'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['additional']['coupon_used'] ) && ! empty( $data['fb']['additional']['coupon_used'] ) ) { ?>
		wfocufbDataPurchaseTrackSynced.coupon_used = '<?php echo esc_js( $data['fb']['additional']['coupon_used'] ); ?>';
	<?php } ?>
	wfocufbDataPurchaseTrackSynced = (typeof wffnAddTrafficParamsToEvent !== "undefined")?wffnAddTrafficParamsToEvent(wfocufbDataPurchaseTrackSynced):wfocufbDataPurchaseTrackSynced;
	fbq('track', 'Purchase', wfocufbDataPurchaseTrackSynced,{'eventID': '<?php echo esc_attr( $event_id ); ?>'});
	<?php if ( isset( $data['fb']['bumps'] ) ) {
		foreach ( $data['fb']['bumps'] as $bump ) { ?>
			wfocufbDataPurchaseTrackSynced.Bump_<?php echo preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', $bump['name'] ) ) ?>_triggered = 'yes';
			wfocufbDataPurchaseTrackSynced.Bump_<?php echo preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', $bump['name'] ) ) ?>_accepted = '<?php echo ( 1 == $bump['converted'] ) ? 'yes' : 'no'; ?>';
		<?php }
	} ?>
	<?php
	if ( $this->is_conversion_api() ) {
		$this->api_events[] = array( 'event' => 'Purchase', 'event_id' => $event_id );
	}
}

if ( false === $this->do_track_fb_synced_purchase() && $this->do_track_fb_purchase_event() && is_array( $data ) && isset( $data['fb'] ) ) {
	$event_id = $this->get_event_id( 'Purchase' );
	?>
	var wfocufbDataPurchaseTrack = {
	'value': '<?php echo esc_js( $data['fb']['total'] ); ?>',
	'currency': '<?php echo esc_js( $data['fb']['currency'] ); ?>',
	'content_name': <?php echo wp_json_encode( implode( ',', $data['fb']['content_name'] ) ); ?>,
	'category_name': <?php echo wp_json_encode( implode( ',', $data['fb']['category_name'] ) ); ?>,
	'num_items': <?php echo esc_js( $data['fb']['num_qty'] ); ?>,
	'transaction_id': '<?php echo esc_js( $data['fb']['transaction_id'] ); ?>',
    'order_id': '<?php echo esc_js( $data['fb']['order_id'] ); ?>'
	};
	wfocufbDataPurchaseTrack = (typeof wffnAddTrafficParamsToEvent !== "undefined")?wffnAddTrafficParamsToEvent(wfocufbDataPurchaseTrack):wfocufbDataPurchaseTrack;

	<?php if ( isset( $data['fb']['additional']['town'] ) && ! empty( $data['fb']['additional']['town'] ) ) { ?>
		wfocufbDataPurchaseTrack.town = '<?php echo esc_js( $data['fb']['additional']['town'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['additional']['country'] ) && ! empty( $data['fb']['additional']['country'] ) ) { ?>
		wfocufbDataPurchaseTrack.country = '<?php echo esc_js( $data['fb']['additional']['country'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['additional']['state'] ) && ! empty( $data['fb']['additional']['state'] ) ) { ?>
		wfocufbDataPurchaseTrack.state = '<?php echo esc_js( $data['fb']['additional']['state'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['additional']['payment'] ) && ! empty( $data['fb']['additional']['payment'] ) ) { ?>
		wfocufbDataPurchaseTrack.payment = '<?php echo esc_js( $data['fb']['additional']['payment'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['additional']['shipping'] ) && ! empty( $data['fb']['additional']['shipping'] ) ) { ?>
		wfocufbDataPurchaseTrack.shipping = '<?php echo esc_js( $data['fb']['additional']['shipping'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['additional']['coupon_name'] ) && ! empty( $data['fb']['additional']['coupon_name'] ) ) { ?>
		wfocufbDataPurchaseTrack.coupon_name = '<?php echo esc_js( $data['fb']['additional']['coupon_name'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['additional']['coupon_used'] ) && ! empty( $data['fb']['additional']['coupon_used'] ) ) { ?>
		wfocufbDataPurchaseTrack.coupon_used = '<?php echo esc_js( $data['fb']['additional']['coupon_used'] ); ?>';
	<?php } ?>
	<?php if ( isset( $data['fb']['bumps'] ) ) {
		foreach ( $data['fb']['bumps'] as $bump ) { ?>
			wfocufbDataPurchaseTrack.Bump_<?php echo preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', $bump['name'] ) ) ?>_triggered = 'yes';
			wfocufbDataPurchaseTrack.Bump_<?php echo preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', $bump['name'] ) ) ?>_accepted = '<?php echo ( 1 == $bump['converted'] ) ? 'yes' : 'no'; ?>';
		<?php }
	} ?>
	fbq('track', 'Purchase', wfocufbDataPurchaseTrack,{'eventID': '<?php echo esc_attr( $event_id ); ?>'});
	<?php
	if ( $this->is_conversion_api() ) {
		$this->api_events[] = array( 'event' => 'Purchase', 'event_id' => $event_id );
	}
} ?>
try {

localStorage.setItem("wfocuH_" + wfocuFBHash, "1");

} catch (exception) {

}
}
