Skip to content

Commit

Permalink
Simplify gtag installatio script.
Browse files Browse the repository at this point in the history
Use `gtag` locally.
  • Loading branch information
tomalec committed Oct 10, 2023
1 parent 1c5010d commit 3dcc524
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions includes/class-wc-google-gtag-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,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_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 = '

$custom_gtag = self::tracker_var();

Check failure on line 292 in includes/class-wc-google-gtag-js.php

View workflow job for this annotation

GitHub Actions / PHP coding standards - diff

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
$gtag_snippet = "(()=>{

Check failure on line 293 in includes/class-wc-google-gtag-js.php

View workflow job for this annotation

GitHub Actions / PHP coding standards - diff

Equals sign not aligned with surrounding assignments; expected 1 space but found 8 spaces

Check failure on line 293 in includes/class-wc-google-gtag-js.php

View workflow job for this annotation

GitHub Actions / PHP coding standards - diff

String "(()=>{\n\t\twindow.dataLayer = window.dataLayer || [];\n\t\t// Publish gtag under custom name to the global scope.\n\t\t// Preserve the custom `arguments.callee.name`, for backward compatibility (not sure if even needed).\n\t\tconst gtag = window." does not require double quotes; use single quotes instead
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." . $custom_gtag . " = function " . $custom_gtag . "(){dataLayer.push(arguments);}

Check failure on line 297 in includes/class-wc-google-gtag-js.php

View workflow job for this annotation

GitHub Actions / PHP coding standards - diff

String " = function " does not require double quotes; use single quotes instead
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 @@ -309,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

0 comments on commit 3dcc524

Please sign in to comment.