简体   繁体   中英

Set up Google Analytics onclick tracking breaks site

I am setting up GA o'clock tracking on my site and it works for must of the places where I inserted it. When I try to put it into the following code it breaks the site - how to integrate the code here correctly to track onclick events?

I used the following code snippet to integrate in the tags where I wanted to track:

onClick="ga('send', 'event', 'AmzClickout', 'PDPCta', '<?php echo esc_attr( $product->get_sku() ); ?>');"

However, if I try to put it in the code below it breaks.

<?php
    /**
     * Loop Add to Cart
     *
     * @author      WooThemes
     * @package     WooCommerce/Templates
     * @version     2.1.0
     */

    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

    global $product;

    echo apply_filters( 'woocommerce_loop_add_to_cart_link',
        sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s">%s</a>',
            esc_url( $product->add_to_cart_url() ),
            esc_attr( $product->id ),
            esc_attr( $product->get_sku() ),
            esc_attr( isset( $quantity ) ? $quantity : 1 ),
            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
            esc_attr( $product->product_type ),
            esc_html( $product->add_to_cart_text() )
        ),
    $product );

Thanks a lot upfront!

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $product;

echo apply_filters( 'woocommerce_loop_add_to_cart_link',
    sprintf( '<a href="%s" onClick="ga(\'send\', \'event\', \'AmzClickout\', \'PDPCta\', \'' . esc_attr( $product->get_sku() ) . '\')" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( $product->id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
        esc_attr( $product->product_type ),
        esc_html( $product->add_to_cart_text() )
    ),
$product );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM