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

Slightly simplify JS construction in WC_Google_Gtag_JS #317

Closed
wants to merge 4 commits into from
Closed
Changes from 3 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
99 changes: 45 additions & 54 deletions includes/class-wc-google-gtag-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,36 +199,28 @@ public static function listing_impression( $product ) {
* @param WC_Product $product
*/
public static function listing_click( $product ) {
$item = array(
'id' => self::get_product_identifier( $product ),
'name' => $product->get_title(),
'category' => self::product_get_category_line( $product ),
'quantity' => 1,
);

$select_content_event_code = self::get_event_code(
'select_content',
array(
'items' => array( $item ),
)
);

$add_to_cart_event_code = self::get_event_code(
'add_to_cart',
array(
'items' => array( $item ),
)
);
$items = [
'items' => [
'id' => self::get_product_identifier( $product ),
'name' => $product->get_title(),
'category' => self::product_get_category_line( $product ),
'quantity' => 1,
],
];

wc_enqueue_js(
"
$( '.product.post-" . esc_js( $product->get_id() ) . ' a , .product.post-' . esc_js( $product->get_id() ) . " button' ).on('click', function() {
if ( false === $(this).hasClass( 'product_type_variable' ) && false === $(this).hasClass( 'product_type_grouped' ) ) {
$add_to_cart_event_code
} else {
$select_content_event_code
}
});"
sprintf(
"$( '.product.post-%s a , .product.post-%1\$s button' ).on('click', function() {
if ( false === $(this).hasClass( 'product_type_variable' ) && false === $(this).hasClass( 'product_type_grouped' ) ) {
%s
} else {
%s
}
});",
esc_js( $product->get_id() ),
self::get_event_code( 'add_to_cart', $items ),
self::get_event_code( 'select_content', $items ),
)
);
}

Expand Down Expand Up @@ -286,23 +278,26 @@ public static function load_analytics( $order = false ) {
$track_404_enabled = '';
if ( 'yes' === self::get( 'ga_404_tracking_enabled' ) && is_404() ) {
// See https://developers.google.com/analytics/devguides/collection/gtagjs/events for reference
$track_404_enabled = self::tracker_var() . "( 'event', '404_not_found', { 'event_category':'error', 'event_label':'page: ' + document.location.pathname + document.location.search + ' referrer: ' + document.referrer });";
$track_404_enabled = "gtag( 'event', '404_not_found', { 'event_category':'error', 'event_label':'page: ' + document.location.pathname + document.location.search + ' referrer: ' + document.referrer });";
}

$gtag_developer_id = '';
if ( ! empty( self::DEVELOPER_ID ) ) {
$gtag_developer_id = self::tracker_var() . "('set', 'developer_id." . self::DEVELOPER_ID . "', true);";
$gtag_developer_id = "gtag('set', 'developer_id." . self::DEVELOPER_ID . "', true);";
}

$gtag_name = self::tracker_var();
$gtag_id = self::get( 'ga_id' );
$gtag_cross_domains = ! empty( self::get( 'ga_linker_cross_domains' ) ) ? array_map( 'esc_js', explode( ',', self::get( 'ga_linker_cross_domains' ) ) ) : array();
$gtag_snippet = '

$gtag_snippet = "(()=>{
window.dataLayer = window.dataLayer || [];
function ' . self::tracker_var() . '(){dataLayer.push(arguments);}
' . self::tracker_var() . "('js', new Date());
// Publish gtag under custom name to the global scope.
// Preserve the custom `arguments.callee.name`, for backward compatibility (not sure if even needed).
const gtag = window.$gtag_name = function $gtag_name(){dataLayer.push(arguments);}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I'm not sure if we or Google are bothered about arguments.callee.name that's pushed to window.dataLayer. If we don't mind pushing always gtag regardless of the custom name in tracker_var, we could simplify it to just `window.
Suggested change
const gtag = window.$gtag_name = function $gtag_name(){dataLayer.push(arguments);}
window." . $custom_gtag . " = function gtag(){dataLayer.push(arguments);}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Or if the tracker_var was just to avoid conflicts with other plugins using the "gtag", and not to allow customization of something very specific. We can hide it in our own namespace/scope. Like
Suggested change
const gtag = window.$gtag_name = function $gtag_name(){dataLayer.push(arguments);}
window.wcgai.gtag = function gtag(){dataLayer.push(arguments);}

And use it as wcgai.gtag( 'event', //... everywhere else

gtag('js', new Date());
$gtag_developer_id

" . self::tracker_var() . "('config', '" . esc_js( $gtag_id ) . "', {
gtag('config', '" . esc_js( $gtag_id ) . "', {
'allow_google_signals': " . ( 'yes' === self::get( 'ga_support_display_advertising' ) ? 'true' : 'false' ) . ",
'link_attribution': " . ( 'yes' === self::get( 'ga_support_enhanced_link_attribution' ) ? 'true' : 'false' ) . ",
'anonymize_ip': " . ( 'yes' === self::get( 'ga_anonymize_enabled' ) ? 'true' : 'false' ) . ",
Expand All @@ -317,7 +312,7 @@ function ' . self::tracker_var() . '(){dataLayer.push(arguments);}
} );

$track_404_enabled
";
})();";

wp_register_script( 'google-tag-manager', 'https://www.googletagmanager.com/gtag/js?id=' . esc_js( $gtag_id ), array( 'google-analytics-opt-out' ), null, false );
wp_add_inline_script( 'google-tag-manager', apply_filters( 'woocommerce_gtag_snippet', $gtag_snippet ) );
Expand Down Expand Up @@ -481,29 +476,25 @@ public function event_tracking_code( $parameters, $selector ) {

$parameters = apply_filters( 'woocommerce_gtag_event_tracking_parameters', $parameters );

$items = '';
if ( 'yes' === self::get( 'ga_enhanced_ecommerce_tracking_enabled' ) ) {
$track_event = sprintf(
self::tracker_var() . "( 'event', %s, { 'event_category': %s, 'event_label': %s, 'items': [ %s ] } );",
$parameters['action'],
$parameters['category'],
$parameters['label'],
$items = sprintf(
", 'items': [ %s ]",
$parameters['item']
);
} else {
$track_event = sprintf(
self::tracker_var() . "( 'event', %s, { 'event_category': %s, 'event_label': %s } );",
$parameters['action'],
$parameters['category'],
$parameters['label']
);
}

wc_enqueue_js(
"
$( '" . $selector . "' ).on( 'click', function() {
" . $track_event . '
});
'
sprintf(
"$( %s ).on( 'click', function() {
%s( 'event', %s, { 'event_category': %s, 'event_label': %s%s } );
});",
$selector,
self::tracker_var(),
$parameters['action'],
$parameters['category'],
$parameters['label'],
$items,
)
);
}

Expand Down