Plugin Directory

Changeset 2623775


Ignore:
Timestamp:
11/03/2021 07:50:28 AM (4 years ago)
Author:
clabsvishnuprasad
Message:

fixing multiple events on ajax and pageload

Location:
customerlabs-actionrecorder
Files:
7 edited
9 copied

Legend:

Unmodified
Added
Removed
  • customerlabs-actionrecorder/tags/1.3.0/ActionRecorder.php

    r2582041 r2623775  
    33Plugin Name: Customerlabs ActionRecorder
    44Description: A simple implementation of e-commerce events tracking for ActionRecorder
    5 Version: 1.2.1
     5Version: 1.3.0
    66License: GPLv2
    77Author: CustomerLabs Digital Solutions Pvt. Ltd.
     
    7373    *
    7474    *
    75     * @param  string  $event       The name of the event to send to CustomerLabs.
    76     * @param  array   $attributes  An array of attributes to send to CustomerLabs.
    77     * @param  string  $cached_event event triggered via ajax call need to be cleared
    78     *
    79     */
    80     public static function track( $event, $attributes = array(), $cached_event ) {
     75    * @param  array  $tracks       An array of events to send to CustomerLabs.
     76    *
     77    */
     78    public static function track( $tracks ) {
    8179
    8280        include_once( dirname( __FILE__ ) . '/templates/track.php' );
     
    429427
    430428        // Track a custom page view event if the current page merits it.
    431         $track = $this->get_current_page_track();
     429        $tracks = $this->get_current_page_track();
    432430        $page  = $this->get_current_page();
    433431
    434         if ( $track ) {
    435             $cached_event = isset( $track['cached_event'] ) ? $track['cached_event'] : false;
    436             self::$instance->cltracker->track( $track['event'], $track['attributes'], $cached_event );
     432        if ( $tracks ) {
     433            self::$instance->cltracker->track( $tracks );
    437434        }
    438435
     
    641638        }
    642639
    643         return apply_filters( 'cltracker_get_current_page_track', $track, $settings, $this );
     640        return apply_filters( 'cltracker_get_current_page_track', [ $track ], $settings, $this );
    644641    }
    645642    /**
  • customerlabs-actionrecorder/tags/1.3.0/Readme.md

    r2582081 r2623775  
    5454## Changelog
    5555
     56= 1.3.0 =
     57* Fixed multiple ajax events
     58* Fixed multiple events tracking on page load
     59
    5660= 1.2.1 =
    5761* Bug Fix settings update issue
     
    6872## Upgrade Notice
    6973
     74= 1.3.0 =
     75Fixed multiple ajax events
     76Fixed multiple events tracking on page load
     77
    7078= 1.2.1 =
    7179Bug Fix settings update issue
  • customerlabs-actionrecorder/tags/1.3.0/Readme.txt

    r2582082 r2623775  
    5454== Changelog ==
    5555
     56= 1.3.0 =
     57* Fixed multiple ajax events
     58* Fixed multiple events tracking on page load
     59
    5660= 1.2.1 =
    5761* Bug Fix settings update issue
     
    6872== Upgrade Notice ==
    6973
     74= 1.3.0 =
     75Fixed multiple ajax events
     76Fixed multiple events tracking on page load
     77
    7078= 1.2.1 =
    7179Bug Fix settings update issue
  • customerlabs-actionrecorder/tags/1.3.0/class.cltracker-cookie.php

    r2060768 r2623775  
    4545
    4646    /**
     47     * Gets cookie value from the client side where name start with
     48     *
     49     * @param string $key Name of the cookie start with
     50     *
     51     * @since 1.0.0
     52     *
     53     */
     54    public static function get_cookies_start_with( $key ) {
     55       
     56        $set = false;
     57        $cookies = array();
     58        $cookie_name = "cltracker_" . $key . '_';
     59        foreach ($_COOKIE as $name => $value) {
     60            if (stripos($name, $cookie_name) === 0) {
     61                $cookies[] = $_COOKIE[$name];
     62                $set = true;
     63            }
     64        }
     65       
     66        print_r($set);
     67       
     68        if (!$set) {
     69            return false;
     70        }
     71
     72        return $cookies;
     73    }
     74
     75    /**
    4776     * Deletes cookie from the client
    4877     *
  • customerlabs-actionrecorder/tags/1.3.0/integrations/ecommerce/woocommerce.php

    r2577817 r2623775  
    4242
    4343        $args = func_get_args();
    44         $track = $args[0];
     44        $tracks = $args[0];
    4545        $settings = $args[1];
    4646
     
    5757                    )
    5858                );
    59             }
    60 
    61         }
    62 
    63         return $track;
     59                array_push($tracks, $track);
     60            }
     61
     62        }
     63
     64        return $tracks;
    6465    }
    6566
     
    7677
    7778        $args  = func_get_args();
    78         $track = $args[0];
     79        $tracks = $args[0];
    7980        $settings = $args[1];
    8081
     
    105106                        )
    106107                    );
    107             }
    108         }
    109 
    110         return $track;
     108                    array_push($tracks, $track);
     109            }
     110        }
     111
     112        return $tracks;
    111113    }
    112114
     
    144146        $image_url = wp_get_attachment_image_url( $image_id, 'full' );
    145147
    146         CLTracker_Cookie::set_cookie( 'added_to_cart', json_encode(
     148        CLTracker_Cookie::set_cookie( 'added_to_cart' . '_' . $id, json_encode(
    147149                array(
    148150                    'product_id'       => $id,
     
    172174        $args = func_get_args();
    173175
    174         $track = $args[0];
     176        $tracks = $args[0];
    175177        $settings = $args[1];
    176178
    177179        if ( $settings['track_add_to_cart'] ) {
    178180
    179             if ( false !== ( $cookie = CLTracker_Cookie::get_cookie( 'added_to_cart' ) ) ) {
     181            if ( false !== ( $cookies = CLTracker_Cookie::get_cookies_start_with( 'added_to_cart' ) ) ) {
    180182           
    181183                if ( ! is_object( WC()->cart ) ) {
    182                     return $track;
     184                    return $tracks;
    183185                }
    184186
    185                 $_product = json_decode(stripslashes($cookie));
    186 
    187                 if ( is_object( $_product ) ) {
    188                     $product  = wc_get_product( $_product->product_id );
    189 
    190                     if ( $product ) {
    191                         $image_id  = $product->get_image_id();
    192                         $image_url = wp_get_attachment_image_url( $image_id, 'full' );
    193                         $item = array(
    194                             'product_id'       => $product->get_id(),
    195                             'product_name'     => $product->get_title(),
    196                             'product_price'    => $product->get_price(),
    197                             'product_quantity' => $_product->product_quantity,
    198                             'product_sku'      => $product->get_sku(),
    199                             'product_image'    => $image_url,
    200                             'product_category' => implode( ', ', wp_list_pluck( wc_get_product_terms( $product->get_id(), 'product_cat' ), 'name' ) ),
    201                         );
    202 
    203                         $track = array(
    204                             'event'      => __( 'Added to cart', 'cltracker' ),
    205                             'attributes' => array(
    206                                 "customProperties" => array(
    207                                     "currency" => get_woocommerce_currency(),
    208                                     "content_type" => "product_group"
     187
     188                if ( is_array( $cookies ) ) {
     189                   
     190                    foreach ( $cookies as $cookie ) {
     191                       
     192                        $_product = json_decode(stripslashes($cookie));
     193                        $product  = wc_get_product( $_product->product_id );
     194
     195                        if ( $product ) {
     196                            $image_id  = $product->get_image_id();
     197                            $image_url = wp_get_attachment_image_url( $image_id, 'full' );
     198                            $item = array(
     199                                'product_id'       => $product->get_id(),
     200                                'product_name'     => $product->get_title(),
     201                                'product_price'    => $product->get_price(),
     202                                'product_quantity' => $_product->product_quantity,
     203                                'product_sku'      => $product->get_sku(),
     204                                'product_image'    => $image_url,
     205                                'product_category' => implode( ', ', wp_list_pluck( wc_get_product_terms( $product->get_id(), 'product_cat' ), 'name' ) ),
     206                            );
     207
     208                            $track = array(
     209                                'event'      => __( 'Added to cart', 'cltracker' ),
     210                                'attributes' => array(
     211                                    "customProperties" => array(
     212                                        "currency" => get_woocommerce_currency(),
     213                                        "content_type" => "product_group"
     214                                    ),
     215                                    "productProperties" => array(
     216                                        $item
     217                                    )
    209218                                ),
    210                                 "productProperties" => array(
    211                                     $item
    212                                 )
    213                             ),
    214                             'cached_event' => 'added_to_cart'
    215                         );
     219                                'cached_event' => 'added_to_cart_' . $_product->product_id
     220                            );
     221                            array_push($tracks, $track);
     222                        }
    216223                    }
    217224                }
     
    220227        }
    221228
    222         return $track;
     229        return $tracks;
    223230    }
    224231   
     
    250257        $product = wc_get_product($cart_item['product_id']);
    251258
    252         CLTracker_Cookie::set_cookie( 'removed_from_cart', json_encode(
     259        CLTracker_Cookie::set_cookie( 'removed_from_cart' . '_' . $cart_item['product_id'], json_encode(
    253260                array(
    254261                    'product_id'       => $cart_item['product_id'],
     
    275282        $args = func_get_args();
    276283
    277         $track = $args[0];
     284        $tracks = $args[0];
    278285        $settings = $args[1];
    279286
    280287        if ( $settings['track_remove_from_cart'] ) {
    281             if ( false !== ( $cookie = CLTracker_Cookie::get_cookie( 'removed_from_cart' ) ) ) {
     288            if ( false !== ( $cookies = CLTracker_Cookie::get_cookies_start_with( 'removed_from_cart' ) ) ) {
    282289
    283290                if ( ! is_object( WC()->cart ) ) {
    284                     return $track;
     291                    return $tracks;
    285292                }
    286293
    287                 $_product  = json_decode( stripslashes($cookie) );
    288 
    289                 if ( is_object( $_product ) ) {
    290                     $product  = wc_get_product( $_product->product_id );
    291 
    292                     if ( $product ) {
    293                         $item = array(
    294                             'product_id'       => $product->get_id(),
    295                             'product_sku'      => $product->get_sku(),
    296                             'product_name'     => $product->get_title(),
    297                             'product_price'    => $product->get_price(),
    298                             'product_quantity' => 0,
    299                             'product_category' => implode( ', ', wp_list_pluck( wc_get_product_terms( $product->get_id(), 'product_cat' ), 'name' ) ),
    300                         );
    301 
    302                         $track = array(
    303                             'event'      => __( 'Removed from cart', 'cltracker' ),
    304                             'attributes' => array(
    305                                 "productProperties" => array(
    306                                     $item
    307                                 )
    308                             ),
    309                             'cached_event' => 'removed_from_cart'
    310                         );
     294               
     295                if ( is_array( $cookies ) ) {
     296                   
     297                    foreach ( $cookies as $cookie ){
     298                        $_product  = json_decode( stripslashes($cookie) );
     299                        $product  = wc_get_product( $_product->product_id );
     300
     301                        if ( $product ) {
     302                            $item = array(
     303                                'product_id'       => $product->get_id(),
     304                                'product_sku'      => $product->get_sku(),
     305                                'product_name'     => $product->get_title(),
     306                                'product_price'    => $product->get_price(),
     307                                'product_quantity' => 0,
     308                                'product_category' => implode( ', ', wp_list_pluck( wc_get_product_terms( $product->get_id(), 'product_cat' ), 'name' ) ),
     309                            );
     310
     311                            $track = array(
     312                                'event'      => __( 'Removed from cart', 'cltracker' ),
     313                                'attributes' => array(
     314                                    "productProperties" => array(
     315                                        $item
     316                                    )
     317                                ),
     318                                'cached_event' => 'removed_from_cart_' . $_product->product_id
     319                            );
     320                            array_push($tracks, $track);
     321                        }
    311322                    }
    312323                }
     
    314325        }
    315326
    316         return $track;
     327        return $tracks;
    317328    }
    318329
     
    327338    public function completed_order() {
    328339        $args  = func_get_args();
    329         $track = $args[0];
     340        $tracks = $args[0];
    330341        $settings = $args[1];
    331342
     
    376387                        )
    377388                    );
    378 
     389                    array_push($tracks, $track);
    379390                }
    380391            }
    381392        }
    382393
    383         return $track;
     394        return $tracks;
    384395    }
    385396
     
    481492
    482493        $args  = func_get_args();
    483         $track = $args[0];
     494        $tracks = $args[0];
    484495        $settings = $args[1];
    485496
     
    487498
    488499            if ( ! is_object( WC()->cart ) ) {
    489                 return $track;
     500                return $tracks;
    490501            }
    491502
    492503            if ( !is_checkout() ) {
    493                 return $track;
     504                return $tracks;
    494505            }
    495506
     
    499510
    500511            if ( count($items) == 0 ){
    501                 return $track;
     512                return $tracks;
    502513            }
    503514
     
    534545                )
    535546            );
    536 
    537         }
    538 
    539         return $track;
     547            array_push($tracks, $track);
     548        }
     549
     550        return $tracks;
    540551    }
    541552
  • customerlabs-actionrecorder/tags/1.3.0/templates/track.php

    r2577640 r2623775  
    11<script type="text/javascript" id="cl_track">
    22window.clWordpressTrack = function(){
    3   _cl.track(<?php echo '"' . esc_js( $event ) . '"' ?><?php if ( ! empty( $attributes ) ) { echo ', ' . json_encode( CLTracker_WordPress::esc_js_deep( $attributes ) ); } else { echo ', {}'; } ?>);
     3<?php
     4    foreach ($tracks as $track) {
     5        if ( $track ) {
     6            $cached_event = isset( $track['cached_event'] ) ? $track['cached_event'] : false;
     7            $event = $track['event'];
     8            $attributes = $track['attributes'];
     9            if ( $cached_event ) :
     10                ?>
     11
     12                    _cl.ajaxurl = "<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>";
     13
     14                    jQuery( document ).ready( function( $ ) {
     15                        var data = {
     16                            action : 'cltracker_unset_cookie',
     17                            key    : '<?php echo esc_js( $cached_event ); ?>',
     18
     19                        },
     20                        success = function( response ) {
     21                            console.log( response );
     22                        };
     23
     24                        $.post( _cl.ajaxurl, data, success );
     25                    });
     26
     27                    <?php
     28                endif;
     29            ?>
     30            _cl.track(<?php echo '"' . esc_js( $event ) . '"' ?><?php if ( ! empty( $attributes ) ) { echo ', ' . json_encode( CLTracker_WordPress::esc_js_deep( $attributes ) ); } else { echo ', {}'; } ?>);
     31        <?php
     32        }
     33    }?>
    434};
    535
    6 
    7 <?php
    8     if ( $cached_event ) :
    9         ?>
    10 
    11         _cl.ajaxurl = "<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>";
    12 
    13         jQuery( document ).ready( function( $ ) {
    14             var data = {
    15                 action : 'cltracker_unset_cookie',
    16                 key    : '<?php echo esc_js( $cached_event ); ?>',
    17 
    18             },
    19             success = function( response ) {
    20                 console.log( response );
    21             };
    22 
    23             $.post( _cl.ajaxurl, data, success );
    24         });
    25 
    26         <?php
    27     endif;
    28 ?>
    29 
    3036</script>
  • customerlabs-actionrecorder/trunk/ActionRecorder.php

    r2582041 r2623775  
    33Plugin Name: Customerlabs ActionRecorder
    44Description: A simple implementation of e-commerce events tracking for ActionRecorder
    5 Version: 1.2.1
     5Version: 1.3.0
    66License: GPLv2
    77Author: CustomerLabs Digital Solutions Pvt. Ltd.
     
    7373    *
    7474    *
    75     * @param  string  $event       The name of the event to send to CustomerLabs.
    76     * @param  array   $attributes  An array of attributes to send to CustomerLabs.
    77     * @param  string  $cached_event event triggered via ajax call need to be cleared
    78     *
    79     */
    80     public static function track( $event, $attributes = array(), $cached_event ) {
     75    * @param  array  $tracks       An array of events to send to CustomerLabs.
     76    *
     77    */
     78    public static function track( $tracks ) {
    8179
    8280        include_once( dirname( __FILE__ ) . '/templates/track.php' );
     
    429427
    430428        // Track a custom page view event if the current page merits it.
    431         $track = $this->get_current_page_track();
     429        $tracks = $this->get_current_page_track();
    432430        $page  = $this->get_current_page();
    433431
    434         if ( $track ) {
    435             $cached_event = isset( $track['cached_event'] ) ? $track['cached_event'] : false;
    436             self::$instance->cltracker->track( $track['event'], $track['attributes'], $cached_event );
     432        if ( $tracks ) {
     433            self::$instance->cltracker->track( $tracks );
    437434        }
    438435
     
    641638        }
    642639
    643         return apply_filters( 'cltracker_get_current_page_track', $track, $settings, $this );
     640        return apply_filters( 'cltracker_get_current_page_track', [ $track ], $settings, $this );
    644641    }
    645642    /**
  • customerlabs-actionrecorder/trunk/Readme.md

    r2582081 r2623775  
    5454## Changelog
    5555
     56= 1.3.0 =
     57* Fixed multiple ajax events
     58* Fixed multiple events tracking on page load
     59
    5660= 1.2.1 =
    5761* Bug Fix settings update issue
     
    6872## Upgrade Notice
    6973
     74= 1.3.0 =
     75Fixed multiple ajax events
     76Fixed multiple events tracking on page load
     77
    7078= 1.2.1 =
    7179Bug Fix settings update issue
  • customerlabs-actionrecorder/trunk/Readme.txt

    r2582082 r2623775  
    5454== Changelog ==
    5555
     56= 1.3.0 =
     57* Fixed multiple ajax events
     58* Fixed multiple events tracking on page load
     59
    5660= 1.2.1 =
    5761* Bug Fix settings update issue
     
    6872== Upgrade Notice ==
    6973
     74= 1.3.0 =
     75Fixed multiple ajax events
     76Fixed multiple events tracking on page load
     77
    7078= 1.2.1 =
    7179Bug Fix settings update issue
  • customerlabs-actionrecorder/trunk/class.cltracker-cookie.php

    r2060768 r2623775  
    4545
    4646    /**
     47     * Gets cookie value from the client side where name start with
     48     *
     49     * @param string $key Name of the cookie start with
     50     *
     51     * @since 1.0.0
     52     *
     53     */
     54    public static function get_cookies_start_with( $key ) {
     55       
     56        $set = false;
     57        $cookies = array();
     58        $cookie_name = "cltracker_" . $key . '_';
     59        foreach ($_COOKIE as $name => $value) {
     60            if (stripos($name, $cookie_name) === 0) {
     61                $cookies[] = $_COOKIE[$name];
     62                $set = true;
     63            }
     64        }
     65       
     66        print_r($set);
     67       
     68        if (!$set) {
     69            return false;
     70        }
     71
     72        return $cookies;
     73    }
     74
     75    /**
    4776     * Deletes cookie from the client
    4877     *
  • customerlabs-actionrecorder/trunk/integrations/ecommerce/woocommerce.php

    r2577817 r2623775  
    4242
    4343        $args = func_get_args();
    44         $track = $args[0];
     44        $tracks = $args[0];
    4545        $settings = $args[1];
    4646
     
    5757                    )
    5858                );
    59             }
    60 
    61         }
    62 
    63         return $track;
     59                array_push($tracks, $track);
     60            }
     61
     62        }
     63
     64        return $tracks;
    6465    }
    6566
     
    7677
    7778        $args  = func_get_args();
    78         $track = $args[0];
     79        $tracks = $args[0];
    7980        $settings = $args[1];
    8081
     
    105106                        )
    106107                    );
    107             }
    108         }
    109 
    110         return $track;
     108                    array_push($tracks, $track);
     109            }
     110        }
     111
     112        return $tracks;
    111113    }
    112114
     
    144146        $image_url = wp_get_attachment_image_url( $image_id, 'full' );
    145147
    146         CLTracker_Cookie::set_cookie( 'added_to_cart', json_encode(
     148        CLTracker_Cookie::set_cookie( 'added_to_cart' . '_' . $id, json_encode(
    147149                array(
    148150                    'product_id'       => $id,
     
    172174        $args = func_get_args();
    173175
    174         $track = $args[0];
     176        $tracks = $args[0];
    175177        $settings = $args[1];
    176178
    177179        if ( $settings['track_add_to_cart'] ) {
    178180
    179             if ( false !== ( $cookie = CLTracker_Cookie::get_cookie( 'added_to_cart' ) ) ) {
     181            if ( false !== ( $cookies = CLTracker_Cookie::get_cookies_start_with( 'added_to_cart' ) ) ) {
    180182           
    181183                if ( ! is_object( WC()->cart ) ) {
    182                     return $track;
     184                    return $tracks;
    183185                }
    184186
    185                 $_product = json_decode(stripslashes($cookie));
    186 
    187                 if ( is_object( $_product ) ) {
    188                     $product  = wc_get_product( $_product->product_id );
    189 
    190                     if ( $product ) {
    191                         $image_id  = $product->get_image_id();
    192                         $image_url = wp_get_attachment_image_url( $image_id, 'full' );
    193                         $item = array(
    194                             'product_id'       => $product->get_id(),
    195                             'product_name'     => $product->get_title(),
    196                             'product_price'    => $product->get_price(),
    197                             'product_quantity' => $_product->product_quantity,
    198                             'product_sku'      => $product->get_sku(),
    199                             'product_image'    => $image_url,
    200                             'product_category' => implode( ', ', wp_list_pluck( wc_get_product_terms( $product->get_id(), 'product_cat' ), 'name' ) ),
    201                         );
    202 
    203                         $track = array(
    204                             'event'      => __( 'Added to cart', 'cltracker' ),
    205                             'attributes' => array(
    206                                 "customProperties" => array(
    207                                     "currency" => get_woocommerce_currency(),
    208                                     "content_type" => "product_group"
     187
     188                if ( is_array( $cookies ) ) {
     189                   
     190                    foreach ( $cookies as $cookie ) {
     191                       
     192                        $_product = json_decode(stripslashes($cookie));
     193                        $product  = wc_get_product( $_product->product_id );
     194
     195                        if ( $product ) {
     196                            $image_id  = $product->get_image_id();
     197                            $image_url = wp_get_attachment_image_url( $image_id, 'full' );
     198                            $item = array(
     199                                'product_id'       => $product->get_id(),
     200                                'product_name'     => $product->get_title(),
     201                                'product_price'    => $product->get_price(),
     202                                'product_quantity' => $_product->product_quantity,
     203                                'product_sku'      => $product->get_sku(),
     204                                'product_image'    => $image_url,
     205                                'product_category' => implode( ', ', wp_list_pluck( wc_get_product_terms( $product->get_id(), 'product_cat' ), 'name' ) ),
     206                            );
     207
     208                            $track = array(
     209                                'event'      => __( 'Added to cart', 'cltracker' ),
     210                                'attributes' => array(
     211                                    "customProperties" => array(
     212                                        "currency" => get_woocommerce_currency(),
     213                                        "content_type" => "product_group"
     214                                    ),
     215                                    "productProperties" => array(
     216                                        $item
     217                                    )
    209218                                ),
    210                                 "productProperties" => array(
    211                                     $item
    212                                 )
    213                             ),
    214                             'cached_event' => 'added_to_cart'
    215                         );
     219                                'cached_event' => 'added_to_cart_' . $_product->product_id
     220                            );
     221                            array_push($tracks, $track);
     222                        }
    216223                    }
    217224                }
     
    220227        }
    221228
    222         return $track;
     229        return $tracks;
    223230    }
    224231   
     
    250257        $product = wc_get_product($cart_item['product_id']);
    251258
    252         CLTracker_Cookie::set_cookie( 'removed_from_cart', json_encode(
     259        CLTracker_Cookie::set_cookie( 'removed_from_cart' . '_' . $cart_item['product_id'], json_encode(
    253260                array(
    254261                    'product_id'       => $cart_item['product_id'],
     
    275282        $args = func_get_args();
    276283
    277         $track = $args[0];
     284        $tracks = $args[0];
    278285        $settings = $args[1];
    279286
    280287        if ( $settings['track_remove_from_cart'] ) {
    281             if ( false !== ( $cookie = CLTracker_Cookie::get_cookie( 'removed_from_cart' ) ) ) {
     288            if ( false !== ( $cookies = CLTracker_Cookie::get_cookies_start_with( 'removed_from_cart' ) ) ) {
    282289
    283290                if ( ! is_object( WC()->cart ) ) {
    284                     return $track;
     291                    return $tracks;
    285292                }
    286293
    287                 $_product  = json_decode( stripslashes($cookie) );
    288 
    289                 if ( is_object( $_product ) ) {
    290                     $product  = wc_get_product( $_product->product_id );
    291 
    292                     if ( $product ) {
    293                         $item = array(
    294                             'product_id'       => $product->get_id(),
    295                             'product_sku'      => $product->get_sku(),
    296                             'product_name'     => $product->get_title(),
    297                             'product_price'    => $product->get_price(),
    298                             'product_quantity' => 0,
    299                             'product_category' => implode( ', ', wp_list_pluck( wc_get_product_terms( $product->get_id(), 'product_cat' ), 'name' ) ),
    300                         );
    301 
    302                         $track = array(
    303                             'event'      => __( 'Removed from cart', 'cltracker' ),
    304                             'attributes' => array(
    305                                 "productProperties" => array(
    306                                     $item
    307                                 )
    308                             ),
    309                             'cached_event' => 'removed_from_cart'
    310                         );
     294               
     295                if ( is_array( $cookies ) ) {
     296                   
     297                    foreach ( $cookies as $cookie ){
     298                        $_product  = json_decode( stripslashes($cookie) );
     299                        $product  = wc_get_product( $_product->product_id );
     300
     301                        if ( $product ) {
     302                            $item = array(
     303                                'product_id'       => $product->get_id(),
     304                                'product_sku'      => $product->get_sku(),
     305                                'product_name'     => $product->get_title(),
     306                                'product_price'    => $product->get_price(),
     307                                'product_quantity' => 0,
     308                                'product_category' => implode( ', ', wp_list_pluck( wc_get_product_terms( $product->get_id(), 'product_cat' ), 'name' ) ),
     309                            );
     310
     311                            $track = array(
     312                                'event'      => __( 'Removed from cart', 'cltracker' ),
     313                                'attributes' => array(
     314                                    "productProperties" => array(
     315                                        $item
     316                                    )
     317                                ),
     318                                'cached_event' => 'removed_from_cart_' . $_product->product_id
     319                            );
     320                            array_push($tracks, $track);
     321                        }
    311322                    }
    312323                }
     
    314325        }
    315326
    316         return $track;
     327        return $tracks;
    317328    }
    318329
     
    327338    public function completed_order() {
    328339        $args  = func_get_args();
    329         $track = $args[0];
     340        $tracks = $args[0];
    330341        $settings = $args[1];
    331342
     
    376387                        )
    377388                    );
    378 
     389                    array_push($tracks, $track);
    379390                }
    380391            }
    381392        }
    382393
    383         return $track;
     394        return $tracks;
    384395    }
    385396
     
    481492
    482493        $args  = func_get_args();
    483         $track = $args[0];
     494        $tracks = $args[0];
    484495        $settings = $args[1];
    485496
     
    487498
    488499            if ( ! is_object( WC()->cart ) ) {
    489                 return $track;
     500                return $tracks;
    490501            }
    491502
    492503            if ( !is_checkout() ) {
    493                 return $track;
     504                return $tracks;
    494505            }
    495506
     
    499510
    500511            if ( count($items) == 0 ){
    501                 return $track;
     512                return $tracks;
    502513            }
    503514
     
    534545                )
    535546            );
    536 
    537         }
    538 
    539         return $track;
     547            array_push($tracks, $track);
     548        }
     549
     550        return $tracks;
    540551    }
    541552
  • customerlabs-actionrecorder/trunk/templates/track.php

    r2577640 r2623775  
    11<script type="text/javascript" id="cl_track">
    22window.clWordpressTrack = function(){
    3   _cl.track(<?php echo '"' . esc_js( $event ) . '"' ?><?php if ( ! empty( $attributes ) ) { echo ', ' . json_encode( CLTracker_WordPress::esc_js_deep( $attributes ) ); } else { echo ', {}'; } ?>);
     3<?php
     4    foreach ($tracks as $track) {
     5        if ( $track ) {
     6            $cached_event = isset( $track['cached_event'] ) ? $track['cached_event'] : false;
     7            $event = $track['event'];
     8            $attributes = $track['attributes'];
     9            if ( $cached_event ) :
     10                ?>
     11
     12                    _cl.ajaxurl = "<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>";
     13
     14                    jQuery( document ).ready( function( $ ) {
     15                        var data = {
     16                            action : 'cltracker_unset_cookie',
     17                            key    : '<?php echo esc_js( $cached_event ); ?>',
     18
     19                        },
     20                        success = function( response ) {
     21                            console.log( response );
     22                        };
     23
     24                        $.post( _cl.ajaxurl, data, success );
     25                    });
     26
     27                    <?php
     28                endif;
     29            ?>
     30            _cl.track(<?php echo '"' . esc_js( $event ) . '"' ?><?php if ( ! empty( $attributes ) ) { echo ', ' . json_encode( CLTracker_WordPress::esc_js_deep( $attributes ) ); } else { echo ', {}'; } ?>);
     31        <?php
     32        }
     33    }?>
    434};
    535
    6 
    7 <?php
    8     if ( $cached_event ) :
    9         ?>
    10 
    11         _cl.ajaxurl = "<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>";
    12 
    13         jQuery( document ).ready( function( $ ) {
    14             var data = {
    15                 action : 'cltracker_unset_cookie',
    16                 key    : '<?php echo esc_js( $cached_event ); ?>',
    17 
    18             },
    19             success = function( response ) {
    20                 console.log( response );
    21             };
    22 
    23             $.post( _cl.ajaxurl, data, success );
    24         });
    25 
    26         <?php
    27     endif;
    28 ?>
    29 
    3036</script>
Note: See TracChangeset for help on using the changeset viewer.