<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">(function ($) {

    var eventGA = [];
    var eventGAProduct = [];
    
    $.fn.isInViewport = function() {
        var elementTop  = $(this).offset().top;
        var elementBottom = elementTop + ( $(this).outerHeight() * 0.75 ) ;

        var viewportTop = $(window).scrollTop();
        var viewportBottom = viewportTop + $(window).height();

        return elementBottom &gt; viewportTop &amp;&amp; ( (elementTop + $(this).outerHeight() * 0.25) &lt; viewportBottom );
    };

    FBPixel = {};

    FBPixel.addProductBeingViewed = function(msg)
    {
        console.log( 'message:' + msg );
        var platter = $('.platter');
        var sku         = platter.find( '[itemprop=sku]' ).text();
        var name        = platter.find( '[itemprop=name]' ).text();
        var category    = platter.find( 'a.product-category' ).text().toLowerCase();
        var price       = platter.find( '[itemprop=price]' ).attr( 'content' );
        var data = {
                    content_ids: [sku], 
                    content_name: name, 
                    content_category: category, 
                    content_type: 'product_group',
                    value: price , 
                    currency:'IDR'
                };
        console.log( 'data:' + JSON.stringify( data ) );

        if( typeof( fbq ) == 'undefined' ){
            console.log( 'No fb pixel' );
            return;
        }

        // https://developers.facebook.com/docs/facebook-pixel/advanced/
        fbq(
            "track", 
            "ViewContent", data  
        );
    }

    FBPixel.addSubmitProductToCart = function(msg)
    {   
        var platter = $('.platter');
        var f = $( 'form.cart' );
        f.on( 'submit', function( event ){
            // event.preventDefault();
            var sku         = platter.find( '[itemprop=sku]' ).text();
            var name        = platter.find( '[itemprop=name]' ).text();
            var category    = platter.find( 'a.product-category' ).text().toLowerCase();
            var price       = platter.find( '[itemprop=price]' ).attr( 'content' );
            var data = {
                        content_ids: [sku], 
                        content_name: name, 
                        content_category: category, 
                        content_type: 'product_group',
                        value: price , 
                        currency:'IDR'
                    };

            console.log( 'data:' + JSON.stringify( data ) );
            if( typeof( fbq ) == 'undefined' ){
                console.log( 'No fb pixel' );
                return;
            }

            // https://developers.facebook.com/docs/facebook-pixel/advanced/
            fbq(
                "track", 
                "AddToCart", data  
            );

        } )
    }

    FBPixel.sendPlacedOrder = function( msg )
    {   
        console.log( 'triggered:' + msg );
        var summary = $( '.checkout-payment-summary' );    
        if( summary.find( '#cart-items' ).length ==0 ){
            console.log( '#cart-items not ready, schedule 1500sc' );
            setTimeout( function(){
                FBPixel.sendPlacedOrder( msg );
            }, 1500 );
        }

        var content_ids = [];
        var contents = [];
        var num_items = 0;
        $( '#cart-items' ).find( 'li[role=listitem]' ).each( function(){
            contents.push( { 'id': $( this ).data( 'product-sku' ), 'quantity' : $( this ).data( 'product-quantity' ) } );
            content_ids.push( $( this ).data( 'product-sku' ) );
            num_items++;
        } );

        var order_id             = summary.find( '[data-order-id]' ).data( 'order-id' );
        var order_revenue        = summary.find( '[data-order-revenue]' ).data( 'order-revenue' );
        var order_tax            = 0;

        console.log( summary.find( '[data-order-tax]' ).length );
        summary.find( '[data-order-tax]' ).each( function(){
            console.log( 'tax' + $( this ).attr( 'id' ) );
            order_tax += $( this ).data( 'order-tax' );
        } )

        var order_shipping       = summary.find( '[data-order-shipping]' ).length? summary.find( '[data-order-shipping]' ).data( 'order-shipping' ):0;
        var order_free_shipping  = summary.find( '[data-order-free-shipping]' ).length? summary.find( '[data-order-free-shipping]' ).data( 'order-free-shipping' ):0;
        var order_coupon         = summary.find( '[data-order-coupon]' ).length? summary.find( '[data-order-coupon]' ).data( 'order-coupon' ):'';
        var order_currency       = summary.find( '[data-order-currency]').length?summary.find( '[data-order-currency]').data( 'order-currency' ):'';

        var data = {
            "content_ids"   : content_ids,
            "contents"      : contents,
            "content_type"  : "product_group",
            "num_items"     : num_items,
            "value"         : order_revenue , 
            "currency"      : "IDR"
        }
        console.log( 'data ' + JSON.stringify( data ) );

        if( typeof( fbq ) == 'undefined' ){
            console.log( 'No fb pixel' );
            return;
        }

        // https://developers.facebook.com/docs/facebook-pixel/reference/
        fbq(
            "track", 
            "InitiateCheckout", data  
        );
    }

    FBPixel.sendCompletedPurchase = function( msg )
    {

        console.log( 'message:' + msg );

        var content_ids     = [];
        var contents        = [];
        var num_items       = 0;
        var order_revenue   = $( '.order-received-details' ).data( 'revenue' );
        $( '.order-received-details .rs-fulfillment-productstrip-content ul' ).find( 'li[role=listitem]' ).each( function(){
            contents.push( { 'id': $( this ).data( 'product-sku' ), 'quantity' : $( this ).data( 'product-quantity' ) } );
            content_ids.push( $( this ).data( 'product-sku' ) );
            num_items++;
        } );

        var data = {
            "content_ids"   : content_ids,
            "contents"      : contents,
            "content_type"  : "product_group",
            "num_items"     : num_items,
            "value"         : order_revenue , 
            "currency"      : "IDR"
        }
        console.log( 'data ' + JSON.stringify( data ) );

        if( typeof( fbq ) == 'undefined' ){
            console.log( 'No fb pixel' );
            return;
        }

        // https://developers.facebook.com/docs/facebook-pixel/reference/
        fbq(
            "track", 
            "Purchase", data  
        );
        

    }

    console.log( 'ecommerce tracking FB Pixel' )

    $( 'body' ).on( 'update_cart_items.GA', function( el, index ){
        // console.log( 'triggered -&gt; ' + JSON.stringify( index ) );
        // addUpdateBackCartItemQuantity( index );
    })

    $( 'body' ).on( 'delete_cart_items.GA', function( el, index ){
        // console.log( 'delete triggered -&gt; ' + JSON.stringify( index ) );
        // deleteCartItemQuantity( index );
    })

    $( 'body' ).on( 'applied_coupon.GA', function( el, coupon ){
        // console.log( 'applied_coupon.GA triggered -&gt; ' + JSON.stringify( coupon ) );
        // sendAppliedCoupon( coupon );
    })

    $( 'body' ).on( 'placed_order_success.GA', function( el ){
        // console.log( 'placed_order_success.GA triggered' );
        // sendCheckOutPlacedOrder();
        // FBPixel.sendPlacedOrder( 'FBPixel:sendPlacedOrder' );
    })

    $( 'body' ).on( 'redirect_payment.GA', function( el ){
        // console.log( 'redirect_payment.GA triggered' );
        // sendCheckOutPlacedOrder();
        // sendPlacedOrder();
    })

    $( 'body' ).on( 'review_product.GA', function( el, data ){
        // console.log( 'triggered review_product.GA -&gt; ' + JSON.stringify( data ) );
        // sendReviewedProduct( data );
    })
    
    $( document ).ready(function() {

        //view
        if( $( 'body' ).hasClass( 'home' ) ){
            // triggerAddPromo();
        }

        if( $( 'body' ).hasClass( 'archive' ) ){
            // addProductsImpression();
            // addProductsClickAction();

            // $( 'body' ).on( 'change', 'input[name=s]', function(){
            //     var data = {
            //         'hitType'       : 'event',
            //         'eventCategory' : 'search',
            //         'eventAction'   : 'click',
            //         'eventLabel'    : $( this ).val(),
            //         'eventValue'    : 1,
            //         'nonInteraction': true,
            //     }
            //     ga("send", data );
            //     console.log( 'send data search ' + JSON.stringify( data ) );
            // } )
        }

        if( $( 'body' ).hasClass( 'single-product' ) ){
            FBPixel.addProductBeingViewed( 'FBPixel:addProductBeingViewed' );
            // addRelatedProductsImpression();
            // addRelatedProductsClickAction();
            FBPixel.addSubmitProductToCart( 'FBPixel:addSubmitProductToCart' );
        }

        if( $( 'body' ).hasClass( 'woocommerce-cart' ) ){
            // addUpdateCartItemQuantity();
            // sendViewedCartData()
        }

        if( $( 'body' ).hasClass( 'provided-shipping-information' ) ){
            // console.log( 'call sendProvidedShippingInformation' );
            // sendProvidedShippingInformation();
        }

        if( $( 'body' ).hasClass( 'selected-payment-method' ) ){
            // console.log( 'call sendSelectedPaymentMethod' );
            // sendSelectedPaymentMethod();

            // console.log( 'call sendSelectedPaymentMethodOption' );
            // sendSelectedPaymentMethodOption();

            // $( 'form.checkout' ).find( 'input[name=payment_method]' ).on( 'click', function( event ){
            //     sendSelectedPaymentMethodOption();                
            // } )
        }

        if( $( 'body' ).hasClass( 'woocommerce-checkout' ) ){

            $( 'body' ).on( 'click', 'input[name="woocommerce_checkout_place_order"]', function(){
                FBPixel.sendPlacedOrder( 'trigger:FBPixel.sendPlacedOrder, by:name=woocommerce_checkout_place_order' ); 
            } )
        }

        if( $( 'body' ).hasClass( 'woocommerce-order-received' ) ){
            FBPixel.sendCompletedPurchase( 'trigger:FBPixel.sendCompletedPurchase' );
        }

        $( window ).scroll(
            function(){
                if( $( 'body' ).hasClass( 'home' ) ){
                    //triggerAddPromo();
                }

                if( $( 'body' ).hasClass( 'archive' ) ){
                    //addProductsImpression();
                }

                if( $( 'body' ).hasClass( 'single-product' ) ){
                    //addRelatedProductsImpression();
                }
        });

    });
    
    

})(jQuery);

</pre></body></html>