Plugin Directory

Changeset 3374198


Ignore:
Timestamp:
10/07/2025 08:56:25 AM (5 months ago)
Author:
gtmserver
Message:

Update to version 2.1.38 from GitHub

Location:
gtm-server-side
Files:
10 edited
1 copied

Legend:

Unmodified
Added
Removed
  • gtm-server-side/tags/2.1.38/README.txt

    r3366307 r3374198  
    44Requires at least: 5.2.0
    55Tested up to: 6.8.0
    6 Stable tag: 2.1.37
     6Stable tag: 2.1.38
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6868== Changelog ==
    6969
     70= 2.1.38 =
     71* Added "select_item" event
     72
    7073= 2.1.37 =
    7174* Fix: submit button
  • gtm-server-side/tags/2.1.38/assets/js/javascript.js

    r3174835 r3374198  
    3636                }
    3737
    38                 pluginGtmServerSide.pushAddToCart(
    39                     pluginGtmServerSide.removePrefixes( el.dataset )
    40                 );
     38                let gtmData    = pluginGtmServerSide.getGtmItemData( el.dataset );
     39                let customData = pluginGtmServerSide.getCustomItemData( el.dataset );
     40
     41                pluginGtmServerSide.pushAddToCart( gtmData );
     42                pluginGtmServerSide.pushSelectItem( gtmData, customData );
    4143            }
    4244        );
     
    5557                }
    5658
    57                 pluginGtmServerSide.pushAddToCart(
    58                     pluginGtmServerSide.removePrefixes( $el.data() )
    59                 );
     59                let gtmData    = pluginGtmServerSide.getGtmItemData( $el.data() );
     60                let customData = pluginGtmServerSide.getCustomItemData( $el.data() );
     61
     62                pluginGtmServerSide.pushAddToCart( gtmData );
     63                pluginGtmServerSide.pushSelectItem( gtmData, customData );
    6064            }
    6165        );
     
    99103
    100104                pluginGtmServerSide.removeFromCart(
    101                     pluginGtmServerSide.removePrefixes( $thisbutton.data() )
     105                    pluginGtmServerSide.getGtmItemData( $thisbutton.data() )
    102106                );
    103107            }
     
    118122
    119123                pluginGtmServerSide.removeFromCart(
    120                     pluginGtmServerSide.removePrefixes( $el.data() )
     124                    pluginGtmServerSide.getGtmItemData( $el.data() )
    121125                );
    122126            }
     
    130134            $elForm.find( '[name^=gtm_]' )
    131135        );
    132         item     = this.removePrefixes( item );
     136        item     = this.getGtmItemData( item );
    133137
    134138        var $elQty = $elForm.find( '[name=quantity]' );
     
    144148            $elForm.find( '[name^=gtm_]' )
    145149        );
    146         item     = this.removePrefixes( item );
     150        item     = this.getGtmItemData( item );
    147151
    148152        var $elQty = $elForm.find( '[name=quantity]' );
     
    243247
    244248                    if ( originalValue < currentValue ) {
    245                         var item         = $this.removePrefixes( elDataset.dataset );
     249                        var item         = $this.getGtmItemData( elDataset.dataset );
    246250                        item['quantity'] = currentValue - originalValue;
    247251
     
    254258
    255259    /**
    256      * Remove field prefixes.
     260     * Return gtm custom data.
    257261     *
    258262     * @param object items List items.
    259263     * @returns object
    260264     */
    261     removePrefixes: function ( items ) {
    262         var item = {};
     265    getGtmItemData: function ( items ) {
     266        return this._getItemData( items, 'gtm_' );
     267    },
     268
     269    /**
     270     * Return gtm custom data.
     271     *
     272     * @param object items List items.
     273     * @returns object
     274     */
     275    getCustomItemData: function ( items ) {
     276        return this._getItemData( items, 'custom_gtm_' );
     277    },
     278
     279    /**
     280     * Return item data.
     281     *
     282     * @param object items List items.
     283     * @returns object
     284     */
     285    _getItemData: function ( items, prefix ) {
     286        var result = {};
    263287        for ( var key in items ) {
    264             if ( 0 !== key.indexOf( 'gtm_' ) ) {
     288            if ( 0 !== key.indexOf( prefix ) ) {
    265289                continue;
    266290            }
    267291
    268             var itemKey     = key.replace( 'gtm_', '' )
    269             item[ itemKey ] = items[key];
    270         }
    271         return item;
     292            var itemKey       = key.replace( prefix, '' )
     293            result[ itemKey ] = items[ key ];
     294        }
     295        return result;
    272296    },
    273297
     
    318342     */
    319343    pushAddToCart: function ( item ) {
     344        this._pushToDataLayer( item, 'add_to_cart', 'product' )
     345    },
     346
     347    /**
     348     * Push add_to_cart to dataLayer.
     349     *
     350     * @param object item List items.
     351     * @param object custom Custom data.
     352     */
     353    pushSelectItem: function ( item, custom ) {
     354        if ( custom?.pagetype ) { // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter
     355            this._pushToDataLayer(
     356                item,
     357                'select_item',
     358                custom.pagetype,
     359                {
     360                    collection_id:  custom?.collection_id,  // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter
     361                    item_list_name: custom?.item_list_name, // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter
     362                }
     363            )
     364        }
     365    },
     366
     367    /**
     368     * Push to dataLayer.
     369     *
     370     * @param object originalItem List items.
     371     * @param string event Event name.
     372     * @param string pagetype Page type name.
     373     * @param object customEcommerce Ecommerce data.
     374     */
     375    _pushToDataLayer: function ( originalItem, event, pagetype, customEcommerce = {} ) {
     376        item = Object.assign( {}, originalItem );
    320377        if ( item.item_id ) {
    321378            item = [ item ];
     
    333390        }
    334391
     392        let eventDataEcommerce = {
     393            'currency': varGtmServerSide.currency,
     394            'value': value.toFixed( 2 ),
     395            'items': items,
     396        }
     397
    335398        var eventData = {
    336             'event':          this.getDataLayerEventName( 'add_to_cart' ),
    337             'ecomm_pagetype': 'product',
    338             'ecommerce': {
    339                 'currency': varGtmServerSide.currency,
    340                 'value': value.toFixed( 2 ),
    341                 'items': items,
    342             },
     399            'event':          this.getDataLayerEventName( event ),
     400            'ecomm_pagetype': pagetype,
     401            'ecommerce': Object.assign( {}, eventDataEcommerce, customEcommerce ),
    343402        };
    344403        if ( varGtmServerSide.user_data ) {
  • gtm-server-side/tags/2.1.38/gtm-server-side.php

    r3366307 r3374198  
    88 *
    99 * @wordpress-plugin
    10  * Plugin Name:       GTM Server Side
     10 * Plugin Name:       Stape Conversion Tracking
    1111 * Plugin URI:        https://wordpress.org/plugins/gtm-server-side/
    1212 * Description:       Enhance conversion tracking by implementing server-side tagging using server Google Tag Manager container. Effortlessly configure data layer events in web GTM, send webhooks, set up custom loader, and extend cookie lifetime.
    13  * Version:           2.1.37
     13 * Version:           2.1.38
    1414 * Author:            Stape
    1515 * Author URI:        https://stape.io
  • gtm-server-side/tags/2.1.38/includes/class-gtm-server-side-admin-settings.php

    r3359782 r3374198  
    6161    public function admin_menu() {
    6262        add_options_page(
    63             __( 'GTM Server Side', 'gtm-server-side' ),
    64             __( 'GTM Server Side', 'gtm-server-side' ),
     63            __( 'Stape Conversion Tracking', 'gtm-server-side' ),
     64            __( 'Stape Conversion Tracking', 'gtm-server-side' ),
    6565            'manage_options',
    6666            GTM_SERVER_SIDE_ADMIN_SLUG,
  • gtm-server-side/tags/2.1.38/includes/class-gtm-server-side-event-addtocart.php

    r3359782 r3374198  
    3636        add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'woocommerce_after_add_to_cart_button' ) );
    3737        add_filter( 'woocommerce_grouped_product_list_column_quantity', array( $this, 'woocommerce_grouped_product_list_column_quantity' ), 10, 2 );
     38
     39        add_filter( 'gtm_server_side_before_html_data_attributes', array( $this, 'format_data_attributes' ) );
     40        add_filter( 'gtm_server_side_after_html_data_attributes', array( $this, 'attach_data_to_event_select_item' ), 20, 3 );
    3841    }
    3942
     
    5255
    5356        $data             = $this->get_item( $item['data'] );
    54         $data             = $this->get_formatted_data_attributes( $data );
     57        $data             = apply_filters( 'gtm_server_side_before_html_data_attributes', $data, 'remove_from_cart', 'woocommerce_cart_item_remove_link' );
    5558        $data['quantity'] = isset( $item['quantity'] ) ? intval( $item['quantity'] ) : 1;
    56         $attrs            = $this->convert_product_data_to_html_attrs( $data );
     59        $data             = $this->convert_product_data_to_html_attrs( $data );
     60        $attrs            = apply_filters( 'gtm_server_side_after_html_data_attributes', $data, 'remove_from_cart', 'woocommerce_cart_item_remove_link' );
    5761        $link             = str_replace( '<a ', '<a ' . join( ' ', $attrs ), $link );
    5862
     
    7882
    7983        $data             = $this->get_item( $product );
    80         $data             = $this->get_formatted_data_attributes( $data );
     84        $data             = apply_filters( 'gtm_server_side_before_html_data_attributes', $data, 'add_to_cart', 'woocommerce_loop_add_to_cart_args' );
    8185        $data['quantity'] = isset( $args['quantity'] ) ? intval( $args['quantity'] ) : 1;
    8286        $data['index']    = isset( $woocommerce_loop['loop'] ) ? intval( $woocommerce_loop['loop'] ) : 1;
    83         $attrs            = $this->convert_product_data_key( $data );
     87        $data             = $this->convert_product_data_key( $data );
     88        $attrs            = apply_filters( 'gtm_server_side_after_html_data_attributes', $data, 'add_to_cart', 'woocommerce_loop_add_to_cart_args' );
    8489
    8590        if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
     
    109114
    110115        $data  = $this->get_item( $product );
    111         $data  = $this->get_formatted_data_attributes( $data );
    112         $attrs = $this->convert_product_data_to_html_attrs( $data );
     116        $data  = apply_filters( 'gtm_server_side_before_html_data_attributes', $data, 'add_to_cart', 'woocommerce_blocks_product_grid_item_html' );
     117        $data  = $this->convert_product_data_to_html_attrs( $data );
     118        $attrs = apply_filters( 'gtm_server_side_after_html_data_attributes', $data, 'add_to_cart', 'woocommerce_blocks_product_grid_item_html' );
    113119        $html  = str_replace( '<li ', '<li ' . join( ' ', $attrs ), $html );
    114120
     
    160166
    161167    /**
     168     * Formatted data attributes.
     169     *
     170     * @param  array $data Attrs.
     171     * @return array
     172     */
     173    public function format_data_attributes( $data ) {
     174        if ( isset( $data['imageUrl'] ) ) {
     175            $data['image-url'] = $data['imageUrl'];
     176            unset( $data['imageUrl'] );
     177        }
     178
     179        return $data;
     180    }
     181
     182    /**
     183     * Attach data to event select item.
     184     *
     185     * @param  array  $data Attrs.
     186     * @param  string $type Type.
     187     * @param  string $caller_hook Caller hook.
     188     * @return array
     189     */
     190    public function attach_data_to_event_select_item( $data, $type, $caller_hook ) {
     191        if ( 'add_to_cart' !== $type ) {
     192            return $data;
     193        }
     194
     195        $first_key = false;
     196        if ( is_array( $data ) ) {
     197            $first_key = array_key_first( $data );
     198        }
     199
     200        if ( false === $first_key ) {
     201            return $data;
     202        }
     203
     204        $pagetype       = null;
     205        $collection_id  = null;
     206        $item_list_name = null;
     207
     208        if ( is_search() ) {
     209            $pagetype = 'search';
     210        } elseif ( is_shop() ) {
     211            $pagetype = 'shop';
     212        } elseif ( is_product_category() ) {
     213            $pagetype = 'category';
     214        } elseif ( is_product_tag() ) {
     215            $pagetype = 'tag';
     216        } elseif ( 'woocommerce_blocks_product_grid_item_html' === $caller_hook ) {
     217            $pagetype = 'grid';
     218        }
     219
     220        if ( is_product_category() || is_product_tag() ) {
     221            $term = get_queried_object();
     222            if ( $term instanceof WP_Term ) {
     223                $collection_id  = $term->term_id;
     224                $item_list_name = $term->name;
     225            }
     226        }
     227
     228        if ( is_int( $first_key ) ) {
     229            $data[] = 'data-custom_gtm_pagetype="' . esc_attr( $pagetype ) . '"';
     230            $data[] = 'data-custom_gtm_collection_id="' . esc_attr( $collection_id ) . '"';
     231            $data[] = 'data-custom_gtm_item_list_name="' . esc_attr( $item_list_name ) . '"';
     232        } else {
     233            $data['data-custom_gtm_pagetype']       = esc_attr( $pagetype );
     234            $data['data-custom_gtm_collection_id']  = esc_attr( $collection_id );
     235            $data['data-custom_gtm_item_list_name'] = esc_attr( $item_list_name );
     236        }
     237
     238        return $data;
     239    }
     240
     241    /**
    162242     * Convert product data key.
    163243     *
     
    203283        return $array;
    204284    }
    205 
    206     /**
    207      * Return formatted data attributes.
    208      *
    209      * @param  array $attrs Attrs.
    210      * @return array
    211      */
    212     private function get_formatted_data_attributes( $attrs ) {
    213 
    214         if ( isset( $attrs['imageUrl'] ) ) {
    215             $attrs['image-url'] = $attrs['imageUrl'];
    216             unset( $attrs['imageUrl'] );
    217         }
    218 
    219         return $attrs;
    220     }
    221285}
  • gtm-server-side/trunk/README.txt

    r3366307 r3374198  
    44Requires at least: 5.2.0
    55Tested up to: 6.8.0
    6 Stable tag: 2.1.37
     6Stable tag: 2.1.38
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6868== Changelog ==
    6969
     70= 2.1.38 =
     71* Added "select_item" event
     72
    7073= 2.1.37 =
    7174* Fix: submit button
  • gtm-server-side/trunk/assets/js/javascript.js

    r3174835 r3374198  
    3636                }
    3737
    38                 pluginGtmServerSide.pushAddToCart(
    39                     pluginGtmServerSide.removePrefixes( el.dataset )
    40                 );
     38                let gtmData    = pluginGtmServerSide.getGtmItemData( el.dataset );
     39                let customData = pluginGtmServerSide.getCustomItemData( el.dataset );
     40
     41                pluginGtmServerSide.pushAddToCart( gtmData );
     42                pluginGtmServerSide.pushSelectItem( gtmData, customData );
    4143            }
    4244        );
     
    5557                }
    5658
    57                 pluginGtmServerSide.pushAddToCart(
    58                     pluginGtmServerSide.removePrefixes( $el.data() )
    59                 );
     59                let gtmData    = pluginGtmServerSide.getGtmItemData( $el.data() );
     60                let customData = pluginGtmServerSide.getCustomItemData( $el.data() );
     61
     62                pluginGtmServerSide.pushAddToCart( gtmData );
     63                pluginGtmServerSide.pushSelectItem( gtmData, customData );
    6064            }
    6165        );
     
    99103
    100104                pluginGtmServerSide.removeFromCart(
    101                     pluginGtmServerSide.removePrefixes( $thisbutton.data() )
     105                    pluginGtmServerSide.getGtmItemData( $thisbutton.data() )
    102106                );
    103107            }
     
    118122
    119123                pluginGtmServerSide.removeFromCart(
    120                     pluginGtmServerSide.removePrefixes( $el.data() )
     124                    pluginGtmServerSide.getGtmItemData( $el.data() )
    121125                );
    122126            }
     
    130134            $elForm.find( '[name^=gtm_]' )
    131135        );
    132         item     = this.removePrefixes( item );
     136        item     = this.getGtmItemData( item );
    133137
    134138        var $elQty = $elForm.find( '[name=quantity]' );
     
    144148            $elForm.find( '[name^=gtm_]' )
    145149        );
    146         item     = this.removePrefixes( item );
     150        item     = this.getGtmItemData( item );
    147151
    148152        var $elQty = $elForm.find( '[name=quantity]' );
     
    243247
    244248                    if ( originalValue < currentValue ) {
    245                         var item         = $this.removePrefixes( elDataset.dataset );
     249                        var item         = $this.getGtmItemData( elDataset.dataset );
    246250                        item['quantity'] = currentValue - originalValue;
    247251
     
    254258
    255259    /**
    256      * Remove field prefixes.
     260     * Return gtm custom data.
    257261     *
    258262     * @param object items List items.
    259263     * @returns object
    260264     */
    261     removePrefixes: function ( items ) {
    262         var item = {};
     265    getGtmItemData: function ( items ) {
     266        return this._getItemData( items, 'gtm_' );
     267    },
     268
     269    /**
     270     * Return gtm custom data.
     271     *
     272     * @param object items List items.
     273     * @returns object
     274     */
     275    getCustomItemData: function ( items ) {
     276        return this._getItemData( items, 'custom_gtm_' );
     277    },
     278
     279    /**
     280     * Return item data.
     281     *
     282     * @param object items List items.
     283     * @returns object
     284     */
     285    _getItemData: function ( items, prefix ) {
     286        var result = {};
    263287        for ( var key in items ) {
    264             if ( 0 !== key.indexOf( 'gtm_' ) ) {
     288            if ( 0 !== key.indexOf( prefix ) ) {
    265289                continue;
    266290            }
    267291
    268             var itemKey     = key.replace( 'gtm_', '' )
    269             item[ itemKey ] = items[key];
    270         }
    271         return item;
     292            var itemKey       = key.replace( prefix, '' )
     293            result[ itemKey ] = items[ key ];
     294        }
     295        return result;
    272296    },
    273297
     
    318342     */
    319343    pushAddToCart: function ( item ) {
     344        this._pushToDataLayer( item, 'add_to_cart', 'product' )
     345    },
     346
     347    /**
     348     * Push add_to_cart to dataLayer.
     349     *
     350     * @param object item List items.
     351     * @param object custom Custom data.
     352     */
     353    pushSelectItem: function ( item, custom ) {
     354        if ( custom?.pagetype ) { // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter
     355            this._pushToDataLayer(
     356                item,
     357                'select_item',
     358                custom.pagetype,
     359                {
     360                    collection_id:  custom?.collection_id,  // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter
     361                    item_list_name: custom?.item_list_name, // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter
     362                }
     363            )
     364        }
     365    },
     366
     367    /**
     368     * Push to dataLayer.
     369     *
     370     * @param object originalItem List items.
     371     * @param string event Event name.
     372     * @param string pagetype Page type name.
     373     * @param object customEcommerce Ecommerce data.
     374     */
     375    _pushToDataLayer: function ( originalItem, event, pagetype, customEcommerce = {} ) {
     376        item = Object.assign( {}, originalItem );
    320377        if ( item.item_id ) {
    321378            item = [ item ];
     
    333390        }
    334391
     392        let eventDataEcommerce = {
     393            'currency': varGtmServerSide.currency,
     394            'value': value.toFixed( 2 ),
     395            'items': items,
     396        }
     397
    335398        var eventData = {
    336             'event':          this.getDataLayerEventName( 'add_to_cart' ),
    337             'ecomm_pagetype': 'product',
    338             'ecommerce': {
    339                 'currency': varGtmServerSide.currency,
    340                 'value': value.toFixed( 2 ),
    341                 'items': items,
    342             },
     399            'event':          this.getDataLayerEventName( event ),
     400            'ecomm_pagetype': pagetype,
     401            'ecommerce': Object.assign( {}, eventDataEcommerce, customEcommerce ),
    343402        };
    344403        if ( varGtmServerSide.user_data ) {
  • gtm-server-side/trunk/gtm-server-side.php

    r3366307 r3374198  
    88 *
    99 * @wordpress-plugin
    10  * Plugin Name:       GTM Server Side
     10 * Plugin Name:       Stape Conversion Tracking
    1111 * Plugin URI:        https://wordpress.org/plugins/gtm-server-side/
    1212 * Description:       Enhance conversion tracking by implementing server-side tagging using server Google Tag Manager container. Effortlessly configure data layer events in web GTM, send webhooks, set up custom loader, and extend cookie lifetime.
    13  * Version:           2.1.37
     13 * Version:           2.1.38
    1414 * Author:            Stape
    1515 * Author URI:        https://stape.io
  • gtm-server-side/trunk/includes/class-gtm-server-side-admin-settings.php

    r3359782 r3374198  
    6161    public function admin_menu() {
    6262        add_options_page(
    63             __( 'GTM Server Side', 'gtm-server-side' ),
    64             __( 'GTM Server Side', 'gtm-server-side' ),
     63            __( 'Stape Conversion Tracking', 'gtm-server-side' ),
     64            __( 'Stape Conversion Tracking', 'gtm-server-side' ),
    6565            'manage_options',
    6666            GTM_SERVER_SIDE_ADMIN_SLUG,
  • gtm-server-side/trunk/includes/class-gtm-server-side-event-addtocart.php

    r3359782 r3374198  
    3636        add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'woocommerce_after_add_to_cart_button' ) );
    3737        add_filter( 'woocommerce_grouped_product_list_column_quantity', array( $this, 'woocommerce_grouped_product_list_column_quantity' ), 10, 2 );
     38
     39        add_filter( 'gtm_server_side_before_html_data_attributes', array( $this, 'format_data_attributes' ) );
     40        add_filter( 'gtm_server_side_after_html_data_attributes', array( $this, 'attach_data_to_event_select_item' ), 20, 3 );
    3841    }
    3942
     
    5255
    5356        $data             = $this->get_item( $item['data'] );
    54         $data             = $this->get_formatted_data_attributes( $data );
     57        $data             = apply_filters( 'gtm_server_side_before_html_data_attributes', $data, 'remove_from_cart', 'woocommerce_cart_item_remove_link' );
    5558        $data['quantity'] = isset( $item['quantity'] ) ? intval( $item['quantity'] ) : 1;
    56         $attrs            = $this->convert_product_data_to_html_attrs( $data );
     59        $data             = $this->convert_product_data_to_html_attrs( $data );
     60        $attrs            = apply_filters( 'gtm_server_side_after_html_data_attributes', $data, 'remove_from_cart', 'woocommerce_cart_item_remove_link' );
    5761        $link             = str_replace( '<a ', '<a ' . join( ' ', $attrs ), $link );
    5862
     
    7882
    7983        $data             = $this->get_item( $product );
    80         $data             = $this->get_formatted_data_attributes( $data );
     84        $data             = apply_filters( 'gtm_server_side_before_html_data_attributes', $data, 'add_to_cart', 'woocommerce_loop_add_to_cart_args' );
    8185        $data['quantity'] = isset( $args['quantity'] ) ? intval( $args['quantity'] ) : 1;
    8286        $data['index']    = isset( $woocommerce_loop['loop'] ) ? intval( $woocommerce_loop['loop'] ) : 1;
    83         $attrs            = $this->convert_product_data_key( $data );
     87        $data             = $this->convert_product_data_key( $data );
     88        $attrs            = apply_filters( 'gtm_server_side_after_html_data_attributes', $data, 'add_to_cart', 'woocommerce_loop_add_to_cart_args' );
    8489
    8590        if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
     
    109114
    110115        $data  = $this->get_item( $product );
    111         $data  = $this->get_formatted_data_attributes( $data );
    112         $attrs = $this->convert_product_data_to_html_attrs( $data );
     116        $data  = apply_filters( 'gtm_server_side_before_html_data_attributes', $data, 'add_to_cart', 'woocommerce_blocks_product_grid_item_html' );
     117        $data  = $this->convert_product_data_to_html_attrs( $data );
     118        $attrs = apply_filters( 'gtm_server_side_after_html_data_attributes', $data, 'add_to_cart', 'woocommerce_blocks_product_grid_item_html' );
    113119        $html  = str_replace( '<li ', '<li ' . join( ' ', $attrs ), $html );
    114120
     
    160166
    161167    /**
     168     * Formatted data attributes.
     169     *
     170     * @param  array $data Attrs.
     171     * @return array
     172     */
     173    public function format_data_attributes( $data ) {
     174        if ( isset( $data['imageUrl'] ) ) {
     175            $data['image-url'] = $data['imageUrl'];
     176            unset( $data['imageUrl'] );
     177        }
     178
     179        return $data;
     180    }
     181
     182    /**
     183     * Attach data to event select item.
     184     *
     185     * @param  array  $data Attrs.
     186     * @param  string $type Type.
     187     * @param  string $caller_hook Caller hook.
     188     * @return array
     189     */
     190    public function attach_data_to_event_select_item( $data, $type, $caller_hook ) {
     191        if ( 'add_to_cart' !== $type ) {
     192            return $data;
     193        }
     194
     195        $first_key = false;
     196        if ( is_array( $data ) ) {
     197            $first_key = array_key_first( $data );
     198        }
     199
     200        if ( false === $first_key ) {
     201            return $data;
     202        }
     203
     204        $pagetype       = null;
     205        $collection_id  = null;
     206        $item_list_name = null;
     207
     208        if ( is_search() ) {
     209            $pagetype = 'search';
     210        } elseif ( is_shop() ) {
     211            $pagetype = 'shop';
     212        } elseif ( is_product_category() ) {
     213            $pagetype = 'category';
     214        } elseif ( is_product_tag() ) {
     215            $pagetype = 'tag';
     216        } elseif ( 'woocommerce_blocks_product_grid_item_html' === $caller_hook ) {
     217            $pagetype = 'grid';
     218        }
     219
     220        if ( is_product_category() || is_product_tag() ) {
     221            $term = get_queried_object();
     222            if ( $term instanceof WP_Term ) {
     223                $collection_id  = $term->term_id;
     224                $item_list_name = $term->name;
     225            }
     226        }
     227
     228        if ( is_int( $first_key ) ) {
     229            $data[] = 'data-custom_gtm_pagetype="' . esc_attr( $pagetype ) . '"';
     230            $data[] = 'data-custom_gtm_collection_id="' . esc_attr( $collection_id ) . '"';
     231            $data[] = 'data-custom_gtm_item_list_name="' . esc_attr( $item_list_name ) . '"';
     232        } else {
     233            $data['data-custom_gtm_pagetype']       = esc_attr( $pagetype );
     234            $data['data-custom_gtm_collection_id']  = esc_attr( $collection_id );
     235            $data['data-custom_gtm_item_list_name'] = esc_attr( $item_list_name );
     236        }
     237
     238        return $data;
     239    }
     240
     241    /**
    162242     * Convert product data key.
    163243     *
     
    203283        return $array;
    204284    }
    205 
    206     /**
    207      * Return formatted data attributes.
    208      *
    209      * @param  array $attrs Attrs.
    210      * @return array
    211      */
    212     private function get_formatted_data_attributes( $attrs ) {
    213 
    214         if ( isset( $attrs['imageUrl'] ) ) {
    215             $attrs['image-url'] = $attrs['imageUrl'];
    216             unset( $attrs['imageUrl'] );
    217         }
    218 
    219         return $attrs;
    220     }
    221285}
Note: See TracChangeset for help on using the changeset viewer.