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 dbdeca6
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions includes/class-wc-google-gtag-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static function listing_click( $product ) {
'name' => $product->get_title(),
'category' => self::product_get_category_line( $product ),
'quantity' => 1,
]
],
];

wc_enqueue_js(
Expand Down 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_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);}
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 dbdeca6

Please sign in to comment.