Skip to content

Commit

Permalink
refactor: remove GA custom-events related code
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek committed Sep 26, 2023
1 parent 4da09c6 commit 1f9f5a4
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 135 deletions.
124 changes: 0 additions & 124 deletions includes/class-newspack-popups-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,113 +826,6 @@ private static function get_form_class( $type, $element_id ) {
return $element_id . $type_index; // Use a unique class name.
}

/**
* Add tracked analytics events to use in Newspack Plugin's newspack_analytics_events filter.
*
* @param object $popup The popup object.
* @param string $body Post body.
* @param string $element_id The id of the popup element.
*/
private static function get_analytics_events( $popup, $body, $element_id ) {
if ( Newspack_Popups::is_preview_request() || ! Newspack_Popups::is_tracking() ) {
return [];
}

$popup_id = $popup['id'];
$segments = array_reduce(
$popup['segments'],
function( $acc, $segment ) {
if ( $segment instanceof \WP_Term ) {
$acc[] = $segment->name;
}
return $acc;
},
[]
);
$event_category = __( 'Newspack Announcement', 'newspack-popups' );
$formatted_placement = ucwords( str_replace( '_', ' ', $popup['options']['placement'] ) );
$default_event_label = sprintf(
// Translators: Analytics label with prompt details (placement, title, ID, targeted segments).
__( '%1$s: %2$s (%3$s) - %4$s', 'newspack-popups' ),
$formatted_placement,
$popup['title'],
$popup_id,
0 < count( $segments ) ? implode( '; ', $segments ) : __( 'Everyone', 'newspack-popups' )
);
$has_link = preg_match( '/<a\s/', $body ) !== 0;
$newspack_form_class = apply_filters( 'newspack_campaigns_form_class', '.newspack-subscribe-form' );
$newspack_form_class = '.' === substr( $newspack_form_class, 0, 1 ) ? substr( $newspack_form_class, 1 ) : $newspack_form_class; // Strip the "." class selector.
$has_register_form = preg_match( '/id="newspack-(register|subscribe)-(.+)"/', $body ) !== 0;
$has_lists_field = preg_match( '/name="lists\[\]"/', $body ) !== 0;
$has_form = preg_match( '/<form\s|mc4wp-form|\[gravityforms\s|' . $newspack_form_class . '/', $body ) !== 0;
$has_dismiss_form = self::is_overlay( $popup );

$analytics_events = [
[
'on' => 'ini-load',
'element' => '#' . esc_attr( $element_id ),
'event_name' => __( 'Load', 'newspack-popups' ),
'non_interaction' => true,
],
[
'on' => 'visible',
'element' => '#' . esc_attr( $element_id ),
'event_name' => __( 'Seen', 'newspack-popups' ),
'non_interaction' => true,
'visibilitySpec' => [
'totalTimeMin' => 500,
],
],
];

if ( $has_link ) {
$analytics_events[] = [
'on' => 'click',
'element' => '#' . esc_attr( $element_id ) . ' a',
'amp_element' => '#' . esc_attr( $element_id ) . ' a',
'event_name' => __( 'Link Click', 'newspack-popups' ),
];
}

if ( $has_form ) {
$analytics_events[] = [
'amp_on' => 'amp-form-submit',
'on' => 'submit',
'element' => '#' . esc_attr( $element_id ) . ' form:not(.popup-dismiss-form)', // Not a dismissal form.
'event_name' => __( 'Form Submission', 'newspack-popups' ),
];
}
if ( $has_dismiss_form ) {
$analytics_events[] = [
'amp_on' => 'amp-form-submit-success',
'on' => 'submit',
'element' => '#' . esc_attr( $element_id ) . ' .popup-dismiss-form',
'event_name' => __( 'Dismissal', 'newspack-popups' ),
'non_interaction' => true,
];
}

foreach ( $analytics_events as &$event ) {
$event_label = $default_event_label;

// If a form submission and the form contains registration + list info, append that to the event label.
if ( isset( $event['amp_on'] ) && 'amp-form-submit' === $event['amp_on'] && $has_register_form ) {
$event_label .= ' | ${formId}';

// If the reg form has a lists[] field, append the value to the event label.
if ( $has_lists_field ) {
$event_label .= ' - ${formFields[lists[]]}';
}
}

$event['id'] = self::get_uniqid();
$event['event_category'] = esc_attr( $event_category );
$event['event_label'] = esc_attr( $event_label );
}

return $analytics_events;
}

/**
* Canonise popups id. The id from WP will be an integer, but AMP does not play well with that and needs a string.
*
Expand Down Expand Up @@ -1068,16 +961,6 @@ public static function generate_inline_popup( $popup ) {
$assigned_segments = Newspack_Segments_Model::get_popup_segments_ids_string( $popup['id'] );
$frequency_config = self::get_frequency_config( $popup );

$analytics_events = self::get_analytics_events( $popup, $body, $element_id );
if ( ! empty( $analytics_events ) ) {
add_filter(
'newspack_analytics_events',
function ( $evts ) use ( $analytics_events ) {
return array_merge( $evts, $analytics_events );
}
);
}

ob_start();
?>
<div
Expand Down Expand Up @@ -1155,13 +1038,6 @@ public static function generate_popup( $popup ) {
$assigned_segments = Newspack_Segments_Model::get_popup_segments_ids_string( $popup['id'] );
$frequency_config = self::get_frequency_config( $popup );

add_filter(
'newspack_analytics_events',
function ( $evts ) use ( $popup, $body, $element_id ) {
return array_merge( $evts, self::get_analytics_events( $popup, $body, $element_id ) );
}
);

$animation_id = 'a_' . $element_id;

ob_start();
Expand Down
11 changes: 0 additions & 11 deletions includes/class-newspack-popups.php
Original file line number Diff line number Diff line change
Expand Up @@ -876,17 +876,6 @@ public static function is_account_related_post( $post ) {
return has_shortcode( $post->post_content, 'woocommerce_my_account' );
}

/**
* Should tracking code be inserted?
* We shouldn't be tracking analytics in the dashboard or on the front-end by admin/editor users.
*/
public static function is_tracking() {
if ( is_admin() || self::is_user_admin() ) {
return false;
}
return true;
}

/**
* Create campaign.
*
Expand Down

0 comments on commit 1f9f5a4

Please sign in to comment.