simonvk
simonvk

Reputation: 76

Add Google Analytics tracking to Woocommerce external/affiliate product

Looking to add a Google Analytics event tracking code to my Woocommerce external products file for my affiliate site (Wordpress).

I found this on a shop, but can't look into its .php file. In HTML it reads:

<a href="thelink" rel="nofollow external" class="single_add_to_cart_button button alt ext-link" onclick="ga('send', 'event', 'Button', 'Buy at SHOP', 'Page title or Product name');
                     ga('send', 'pageview', 'merchant-button-click');" title="" data-wpel-target="_blank">Buy at SHOP</a>

I'd like this too, but not entirely sure how to translate this in to my current external.php file.

Currently the external.php file looks like this:

?>

<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

<p class="cart">
    <a href="<?php echo esc_url( $product_url ); ?>" rel="nofollow"     class="single_add_to_cart_button button alt"><?php echo $button_text; ?></a>
</p>

<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>

What I'd like is pretty much the same as what I found on that shop, whereas after the 'send', 'event' bit I'd like:

Category: Button

Action: The button's text (e.g. Buy at SHOP name)

Label: Product or page title

PS - I run the Yoast Google Analytics plugin which does this thing for me automatically, however this is only done to blog articles not to the Woocommerce (external) products custom post type.

Upvotes: 0

Views: 1471

Answers (1)

simonvk
simonvk

Reputation: 76

Managed to do this in combination with Google Tag Manager.

In my external.php I added the following for it to communicate with GTM:

<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

<p class="cart">
<a href="<?php echo esc_url( $product_url ); ?>" target="_blank" rel="nofollow"     class="single_add_to_cart_button button alt"  onClick="dataLayer.push({
'event': 'gaEvent',

'eventCategory': 'External link',

'eventAction': '<?php echo $button_text; ?>',

'eventLabel': '<?php echo get_the_title(); ?>'

});"  >   <?php echo $button_text; ?></a>
</p>

<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>

In GTM I made a tracking event tag, along with macros (for Action, Label, Category - to be showin in Google Analytics) and a rule to make sure all was fired from GTM to Google Analytics.

Should there be any Dutch people reading this, the following link helped me out quite well: http://www.easy.nl/2012/10/google-tag-manager/

Upvotes: 0

Related Questions