Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tracking): handling user role in pixel #3137

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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