Skip to content

Commit

Permalink
fix: support frontend events
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpeixe committed Sep 11, 2024
1 parent 767f480 commit 617cbe5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 47 deletions.
45 changes: 3 additions & 42 deletions includes/data-events/class-memberships.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,45 +127,6 @@ public static function register_listeners() {
);
}

/**
* Recursively get the unique block names from the post content.
*
* @param array $blocks The blocks.
*
* @return array
*/
private static function get_block_names_recursive( $blocks ) {
$block_names = [];
foreach ( $blocks as $block ) {
if ( ! empty( $block['blockName'] ) ) {
$block_names[] = $block['blockName'];
}
if ( ! empty( $block['innerBlocks'] ) ) {
$block_names = array_merge( $block_names, self::get_block_names_recursive( $block['innerBlocks'] ) );
}
}
return array_unique( $block_names );
}

/**
* Get common metadata to be sent with all gate interaction events.
*
* @return array {
* The gate metadata.
*
* @type int $gate_post_id The gate post ID.
* @type array $gate_blocks Names of unique blocks in the gate post.
* }
*/
private static function get_gate_metadata() {
$post_id = NewspackMemberships::get_gate_post_id();
$blocks = self::get_block_names_recursive( parse_blocks( get_post_field( 'post_content', $post_id ) ) );
return [
'gate_post_id' => $post_id,
'gate_blocks' => $blocks,
];
}

/**
* A listener for the registration block form submission
*
Expand All @@ -184,7 +145,7 @@ public static function registration_submission( $email, $authenticate, $user_id,
return;
}
$data = array_merge(
self::get_gate_metadata(),
NewspackMemberships::get_gate_metadata(),
[
'action' => self::FORM_SUBMISSION,
'action_type' => 'registration',
Expand Down Expand Up @@ -217,7 +178,7 @@ public static function registration_submission_with_status( $email, $authenticat
$action = self::FORM_SUBMISSION_FAILURE;
}
$data = array_merge(
self::get_gate_metadata(),
NewspackMemberships::get_gate_metadata(),
[
'action' => $action,
'action_type' => 'registration',
Expand All @@ -243,7 +204,7 @@ private static function get_order_data( $order_id, $order ) {
}
$item = array_shift( $order->get_items() );
$data = array_merge(
self::get_gate_metadata(),
NewspackMemberships::get_gate_metadata(),
[
'action_type' => 'paid_membership',
'order_id' => $order_id,
Expand Down
8 changes: 4 additions & 4 deletions includes/data-events/connectors/ga4/class-ga4.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,10 @@ public static function handle_gate_interaction( $params, $data ) {
$params['product_id'] = $data['product_id'] ?? '';
$params['amount'] = $data['amount'] ?? '';
$params['currency'] = $data['currency'] ?? '';
// Check for meaningful blocks.
$params['gate_has_donation_block'] = in_array( 'newspack-blocks/donate', $data['gate_blocks'] ) ? 'yes' : 'no';
$params['gate_has_registration_block'] = in_array( 'newspack/reader-registration', $data['gate_blocks'] ) ? 'yes' : 'no';
$params['gate_has_checkout_button'] = in_array( 'newspack-blocks/checkout-button', $data['gate_blocks'] ) ? 'yes' : 'no';
// Meaningful blocks.
$params['gate_has_donation_block'] = $data['gate_has_donation_block'];
$params['gate_has_registration_block'] = $data['gate_has_registration_block'];
$params['gate_has_checkout_button'] = $data['gate_has_checkout_button'];
return $params;
}

Expand Down
48 changes: 48 additions & 0 deletions includes/plugins/wc-memberships/class-memberships.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ public static function enqueue_scripts() {
true
);
\wp_script_add_data( $handle, 'async', true );
\wp_localize_script(
$handle,
'newspack_memberships_gate',
[
'metadata' => self::get_gate_metadata(),
]
);
\wp_enqueue_style(
$handle,
Newspack::plugin_url() . '/dist/memberships-gate.css',
Expand Down Expand Up @@ -294,6 +301,47 @@ public static function get_gate_post_id( $post_id = null ) {
return $gate_post_id ? $gate_post_id : false;
}

/**
* Recursively get the unique block names from the post content.
*
* @param array $blocks The blocks.
*
* @return array
*/
private static function get_block_names_recursive( $blocks ) {
$block_names = [];
foreach ( $blocks as $block ) {
if ( ! empty( $block['blockName'] ) ) {
$block_names[] = $block['blockName'];
}
if ( ! empty( $block['innerBlocks'] ) ) {
$block_names = array_merge( $block_names, self::get_block_names_recursive( $block['innerBlocks'] ) );
}
}
return array_unique( $block_names );
}

/**
* Get gate metadata to be used for analytics purposes.
*
* @return array {
* The gate metadata.
*
* @type int $gate_post_id The gate post ID.
* @type array $gate_blocks Names of unique blocks in the gate post.
* }
*/
public static function get_gate_metadata() {
$post_id = self::get_gate_post_id();
$blocks = self::get_block_names_recursive( parse_blocks( get_post_field( 'post_content', $post_id ) ) );
return [
'gate_post_id' => $post_id,
'gate_has_donation_block' => in_array( 'newspack-blocks/donate', $blocks ) ? 'yes' : 'no',
'gate_has_registration_block' => in_array( 'newspack/reader-registration', $blocks ) ? 'yes' : 'no',
'gate_has_checkout_button' => in_array( 'newspack-blocks/checkout-button', $blocks ) ? 'yes' : 'no',
];
}

/**
* Get the gate post object for the given plan.
*
Expand Down
17 changes: 16 additions & 1 deletion src/memberships-gate/gate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* globals newspack_memberships_gate */
/**
* Internal dependencies
*/
Expand Down Expand Up @@ -44,6 +45,20 @@ function addFormInputs( gate ) {
} );
}

/**
* Get the full event payload for GA4.
*
* @param {Array} payload The event payload.
*
* @return {Array} The full event payload
*/
function getEventPayload( payload ) {
return {
...newspack_memberships_gate.metadata,
...payload,
}
}

/**
* Handle when the gate is seen.
*/
Expand All @@ -56,7 +71,7 @@ function handleSeen( gate ) {
action: 'seen',
};
if ( 'function' === typeof window.gtag && payload ) {
window.gtag( 'event', eventName, payload );
window.gtag( 'event', eventName, getEventPayload( payload ) );
}
}

Expand Down

0 comments on commit 617cbe5

Please sign in to comment.