Skip to content

Commit

Permalink
fix(tracking): handling user role in pixel (#3137)
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek authored May 27, 2024
1 parent 3d70a1d commit a041764
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
5 changes: 4 additions & 1 deletion includes/tracking/class-meta-pixel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function __construct() {
* Print the pixels' codes.
*/
public function print_code_snippets() {
if ( ! $this->is_configured() ) {
return;
}
add_action( 'wp_head', [ $this, 'print_head_snippet' ], 100 );
add_action( 'wp_footer', [ $this, 'print_footer_snippet' ] );
}
Expand All @@ -38,7 +41,7 @@ private static function get_event_params() {
$current_user = wp_get_current_user();
$event_params = [
'page_title' => get_the_title(),
'user_role' => empty( $current_user->roles ) ? 'guest' : $current_user->roles[0],
'user_role' => empty( $current_user->roles ) ? 'guest' : reset( $current_user->roles ),
'event_url' => home_url( $wp->request ),
];

Expand Down
21 changes: 8 additions & 13 deletions includes/tracking/class-pixel.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public function get_option() {
}

/**
* Checks if option is active
* Checks if pixel should be printed.
*
* @return boolean
*/
public function is_active() {
return $this->get_option()['active'];
public function is_configured() {
return ! empty( $this->get_pixel_id() );
}

/**
Expand All @@ -86,7 +86,7 @@ public function is_active() {
* @return string
*/
public function get_pixel_id() {
if ( ! $this->is_active() ) {
if ( ! $this->get_option()['active'] ) {
return '';
}
return $this->get_option()['pixel_id'];
Expand Down Expand Up @@ -145,14 +145,10 @@ public function is_amp() {
* @return void
*/
public function create_js_snippet( $payload ) {
if ( $this->is_amp() ) {
if ( $this->is_amp() || ! $this->is_configured() ) {
return;
}
$pixel_id = $this->get_pixel_id();
if ( empty( $pixel_id ) ) {
return;
}
echo str_replace( '__PIXEL_ID__', $pixel_id, $payload ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo str_replace( '__PIXEL_ID__', $this->get_pixel_id(), $payload ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
Expand All @@ -162,12 +158,11 @@ public function create_js_snippet( $payload ) {
* @return void
*/
public function create_noscript_snippet( $payload ) {
$pixel_id = $this->get_pixel_id();
if ( empty( $pixel_id ) ) {
if ( ! $this->is_configured() ) {
return;
}
// If AMP plugin is enabled, it will convert the image into a <amp-pixel> tag.
echo '<noscript>' . str_replace( '__PIXEL_ID__', $pixel_id, $payload ) . '</noscript>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<noscript>' . str_replace( '__PIXEL_ID__', $this->get_pixel_id(), $payload ) . '</noscript>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
Expand Down

0 comments on commit a041764

Please sign in to comment.