Skip to content

Commit

Permalink
Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Nov 23, 2023
1 parent 85ac08c commit 8af7d16
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 59 deletions.
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Find out about what's new in our [our release post](https://gtmkit.com/gtm-kit-1

#### Enhancements:
* Added option to specify the Google Tag Manager environment.
* Performance optimization through optimized database queries and general code improvements.

#### Bugfixes:
* Add missing _sbp cookie in the cookie keeper.
Expand Down
24 changes: 12 additions & 12 deletions src/Frontend/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public function __construct( Options $options ) {
*/
public static function register( Options $options ): void {
$page = new Frontend( $options );
$container_active = Options::init()->get( 'general', 'container_active' );
$noscript_implementation = Options::init()->get( 'general', 'noscript_implementation' );
$container_active = $options->get( 'general', 'container_active' );
$noscript_implementation = $options->get( 'general', 'noscript_implementation' );

if ( empty( $options->get( 'general', 'just_the_container' ) ) ) {
add_action( 'wp_enqueue_scripts', [ $page, 'enqueue_settings_and_data_script' ], 1, 0 );
Expand All @@ -55,7 +55,7 @@ public static function register( Options $options ): void {

if ( $container_active ) {
add_action( 'wp_enqueue_scripts', [ $page, 'enqueue_header_script' ] );
} elseif ( Options::init()->get( 'general', 'console_log' ) ) {
} elseif ( $options->get( 'general', 'console_log' ) ) {
add_action( 'wp_head', [ $page, 'container_disabled' ] );
}

Expand All @@ -76,7 +76,7 @@ public static function register( Options $options ): void {
public function enqueue_settings_and_data_script(): void {
$settings = [
'datalayer_name' => $this->datalayer_name,
'console_log' => Options::init()->get( 'general', 'console_log' ),
'console_log' => $this->options->get( 'general', 'console_log' ),
];

ob_start();
Expand Down Expand Up @@ -125,13 +125,13 @@ public function enqueue_datalayer_content(): void {
*/
public function enqueue_header_script(): void {

$gtm_id = Options::init()->get( 'general', 'gtm_id' );
$gtm_id = $this->options->get( 'general', 'gtm_id' );

if ( empty( $gtm_id ) ) {
return;
}

$script_implementation = (int) Options::init()->get( 'general', 'script_implementation' );
$script_implementation = (int) $this->options->get( 'general', 'script_implementation' );

ob_start();

Expand Down Expand Up @@ -171,10 +171,10 @@ function (cb) {
* @param string $gtm_id The GTM container ID.
*/
public function get_gtm_script( string $gtm_id ): void {
$domain = Options::init()->get( 'general', 'sgtm_domain' ) ? Options::init()->get( 'general', 'sgtm_domain' ) : 'www.googletagmanager.com';
$loader = Options::init()->get( 'general', 'sgtm_container_identifier' ) ? Options::init()->get( 'general', 'sgtm_container_identifier' ) : 'gtm';
$domain = $this->options->get( 'general', 'sgtm_domain' ) ? $this->options->get( 'general', 'sgtm_domain' ) : 'www.googletagmanager.com';
$loader = $this->options->get( 'general', 'sgtm_container_identifier' ) ? $this->options->get( 'general', 'sgtm_container_identifier' ) : 'gtm';

if ( Options::init()->get( 'general', 'sgtm_cookie_keeper' ) ) {
if ( $this->options->get( 'general', 'sgtm_cookie_keeper' ) ) {
echo "/* Google Tag Manager with Stape.io Cookie Keeper */\n";
echo '!function(){"use strict";function e(e,t,o){return void 0===t&&(t=""),"cookie"===e?function(e){for(var t=0,o=document.cookie.split(";");t<o.length;t++){var r=o[t].split("=");if(r[0].trim()===e)return r[1]}}(t):"localStorage"===e?(r=t,localStorage.getItem(r)):"jsVariable"===e?window[t]:"cssSelector"===e?(n=t,i=o,a=document.querySelector(n),i?null==a?void 0:a.getAttribute(i):null==a?void 0:a.textContent):void console.warn("invalid uid source",e);var r,n,i,a}!function(t,o,r,n,i,a,c,l,s,u){var d,v,E,I;try{v=l&&(E=navigator.userAgent,(I=/Version\/([0-9\._]+)(.*Mobile)?.*Safari.*/.exec(E))&&parseFloat(I[1])>=16.4)?e(l,"_sbp",""):void 0}catch(e){console.error(e)}var g=t;g[n]=g[n]||[];g[n].push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var m=o.getElementsByTagName(r)[0],T=v?"&bi="+encodeURIComponent(v):"",_=o.createElement(r),f=v?"kp"+c:c,dl=n!="dataLayer"?"&l="+n:"";_.async=!0,_.src="https://' . esc_attr( $domain ) . '/"+f+".js?id=' . esc_attr( $gtm_id ) . '"+dl+T' . ( ( ! empty( Options::init()->get( 'general', 'gtm_auth' ) ) && ! empty( Options::init()->get( 'general', 'gtm_preview' ) ) ) ? "+'&gtm_auth=" . esc_attr( Options::init()->get( 'general', 'gtm_auth' ) ) . '&gtm_preview=' . esc_attr( Options::init()->get( 'general', 'gtm_preview' ) ) . "&gtm_cookies_win=x'" : '' ) . ',null===(d=m.parentNode)||void 0===d||d.insertBefore(_,m)}(window,document,"script","' . esc_js( $this->datalayer_name ) . '",0,0,"' . esc_attr( $loader ) . '","cookie")}();';
echo "\n/* End Google Tag Manager */\n";
Expand All @@ -184,7 +184,7 @@ public function get_gtm_script( string $gtm_id ): void {
echo "new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],\n";
echo "j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=\n";
echo "'https://" . esc_attr( $domain ) . '/' . esc_attr( $loader ) . ".js?id='+i+dl";
echo ( ! empty( Options::init()->get( 'general', 'gtm_auth' ) ) && ! empty( Options::init()->get( 'general', 'gtm_preview' ) ) ) ? "+'&gtm_auth=" . esc_attr( Options::init()->get( 'general', 'gtm_auth' ) ) . '&gtm_preview=' . esc_attr( Options::init()->get( 'general', 'gtm_preview' ) ) . "&gtm_cookies_win=x'" : '';
echo ( ! empty( $this->options->get( 'general', 'gtm_auth' ) ) && ! empty( $this->options->get( 'general', 'gtm_preview' ) ) ) ? "+'&gtm_auth=" . esc_attr( $this->options->get( 'general', 'gtm_auth' ) ) . '&gtm_preview=' . esc_attr( $this->options->get( 'general', 'gtm_preview' ) ) . "&gtm_cookies_win=x'" : '';
echo ";f.parentNode.insertBefore(j,f);\n";
echo "})(window,document,'script','" . esc_js( $this->datalayer_name ) . "','" . esc_attr( $gtm_id ) . "');\n";
echo "/* End Google Tag Manager */\n";
Expand Down Expand Up @@ -231,7 +231,7 @@ public static function get_body_script(): void {
/**
* Console warning
*/
public static function container_disabled(): void {
public function container_disabled(): void {
echo '<script>console.warn("[GTM Kit] Google Tag Manager container is disabled.");</script>';
}

Expand Down Expand Up @@ -259,7 +259,7 @@ public function wp_rocket_exclude_javascript( array $pattern ): array {
*/
public function dns_prefetch( array $hints, string $relation_type ): array {

$domain = Options::init()->get( 'general', 'sgtm_domain' ) ? Options::init()->get( 'general', 'sgtm_domain' ) : 'www.googletagmanager.com';
$domain = $this->options->get( 'general', 'sgtm_domain' ) ? $this->options->get( 'general', 'sgtm_domain' ) : 'www.googletagmanager.com';

if ( 'dns-prefetch' === $relation_type ) {
$hints[] = '//' . $domain;
Expand Down
53 changes: 16 additions & 37 deletions src/Integration/AbstractEcommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,48 +120,27 @@ public function get_primary_product_category( int $product_id, string $taxonomy
* @return array The category path elements as an array.
*/
public function get_category_breadcrumb( int $category_id, string $taxonomy ): array {
$category_hierarchy = [];

$category = get_term( $category_id, $taxonomy );
static $categories = [];

if ( ! $category ) {
return $category_hierarchy;
if ( isset( $categories[ $category_id ] ) ) {
return $categories[ $category_id ];
}

$parents = get_ancestors( $category_id, $taxonomy, 'taxonomy' );

array_unshift( $parents, $category_id );

foreach ( array_reverse( $parents ) as $category_id ) {
$parent = get_term( $category_id, $taxonomy );
$category_hierarchy[] = $parent->name;

$category_hierarchy = [];
$category = get_term( $category_id, $taxonomy );
if ( $category ) {
$parents = get_ancestors( $category_id, $taxonomy, 'taxonomy' );
array_unshift( $parents, $category_id );
foreach ( $parents as $category_id ) {
$parent = get_term( $category_id, $taxonomy );
if ( $parent ) {
array_unshift( $category_hierarchy, $parent->name );
}
}
}
$categories[ $category_id ] = $category_hierarchy;

return $category_hierarchy;
}

/**
* Get product term value.
*
* @param int $product_id A product ID.
* @param string $taxonomy The taxonomy slug.
*
* @return string Returns the first assigned taxonomy value.
*/
public function get_product_term( int $product_id, string $taxonomy ): string {

$product_terms = wp_get_post_terms(
$product_id,
$taxonomy,
[
'orderby' => 'parent',
'order' => 'ASC',
]
);

return ( is_array( $product_terms ) && count( $product_terms ) ) ? $product_terms[0]->name : '';
}
return $category_hierarchy; }

/**
* Prefix an item ID
Expand Down
17 changes: 7 additions & 10 deletions src/Integration/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ public function get_global_settings( array $global_settings ): array {

$global_settings['wc']['use_sku'] = (bool) $this->options->get( 'integrations', 'woocommerce_use_sku' );
$global_settings['wc']['pid_prefix'] = $this->prefix_item_id();
$global_settings['wc']['add_shipping_info']['config'] = (int) Options::init()->get( 'integrations', 'woocommerce_shipping_info' );
$global_settings['wc']['add_payment_info']['config'] = (int) Options::init()->get( 'integrations', 'woocommerce_payment_info' );
$global_settings['wc']['view_item']['config'] = (int) Options::init()->get( 'integrations', 'woocommerce_variable_product_tracking' );
$global_settings['wc']['view_item_list']['config'] = (int) Options::init()->get( 'integrations', 'woocommerce_view_item_list_limit' );
$global_settings['wc']['add_shipping_info']['config'] = (int) $this->options->get( 'integrations', 'woocommerce_shipping_info' );
$global_settings['wc']['add_payment_info']['config'] = (int) $this->options->get( 'integrations', 'woocommerce_payment_info' );
$global_settings['wc']['view_item']['config'] = (int) $this->options->get( 'integrations', 'woocommerce_variable_product_tracking' );
$global_settings['wc']['view_item_list']['config'] = (int) $this->options->get( 'integrations', 'woocommerce_view_item_list_limit' );
$global_settings['wc']['text'] = [
'wp-block-handpicked-products' => __( 'Handpicked Products', 'gtm-kit' ),
'wp-block-product-best-sellers' => __( 'Best Sellers', 'gtm-kit' ),
Expand Down Expand Up @@ -360,7 +360,7 @@ public function get_datalayer_content_product_page( array $data_layer ): array {
$data_layer['pageType'] = 'product-page';
}

if ( $product->get_type() === 'variable' && (int) Options::init()->get( 'integrations', 'woocommerce_variable_product_tracking' ) === 2 ) {
if ( $product->get_type() === 'variable' && (int) $this->options->get( 'integrations', 'woocommerce_variable_product_tracking' ) === 2 ) {
return $data_layer;
}

Expand Down Expand Up @@ -657,10 +657,7 @@ public function get_item_data( WC_Product $product, array $additional_item_attri
];

if ( $this->options->get( 'integrations', 'woocommerce_brand' ) ) {
$item_data['item_brand'] = $this->get_product_term(
$product_id_to_query,
$this->options->get( 'integrations', 'woocommerce_brand' )
);
$item_data['item_brand'] = $product->get_attribute( $this->options->get( 'integrations', 'woocommerce_brand' ) );
}

if ( $this->options->get( 'integrations', 'woocommerce_google_business_vertical' ) ) {
Expand Down Expand Up @@ -992,7 +989,7 @@ public function cart_item_remove_link( string $woocommerce_cart_item_remove_link
* @return string
*/
public function prefix_item_id( string $item_id = '' ): string {
return Options::init()->get( 'integrations', 'woocommerce_product_id_prefix' ) . $item_id;
return $this->options->get( 'integrations', 'woocommerce_product_id_prefix' ) . $item_id;
}

/**
Expand Down

0 comments on commit 8af7d16

Please sign in to comment.