See the best WordPress Black Friday Deals collection and get massive savings on your favorite WordPress services, themes, and plugins! View Deals

Professional WordPress themes for your business and personal sites

Home Blog How to track WooCommerce external/affiliate product link clicks in Google Analytics

How to track WooCommerce external/affiliate product link clicks in Google Analytics

Last updated: 12 Comments

Today we dive into exactly how to track WooCommerce external/affiliate product link clicks in Google Analytics. WooCommerce is a pretty kickass eCommerce platform for WordPress with full featured Add to Cart/Checkout functionality. But many WooCommerce users sell affiliate products via third party websites like Amazon. In this scenario WooCommerce gives us a great solution to managing our product catalog and when customers click on the “Buy now” button they’re redirected to whatever link you specify. In WooCommerce these are known as “External/Affiliate” products.

External/Affiliate Products in WooCommerce
External/Affiliate Products in WooCommerce

For products sold internally within WooCommerce there are a plethora of excellent Google Analytics plugins that give you tons of data about tracking conversion events. But what about tracking conversion for external/affiliate products?

How to track conversion events for External/Affiliate products in WooCommerce

It would be pretty tricky to actually track the sale of a product on the external site unless you have a close relationship with the external site and they’d be happy to add a tracking pixel/conversion code to their order confirmation page for you. This will be especially true of the eCommerce giants like Amazon. (Good luck convincing Jeff Bezos to add your conversion pixel on the Amazon order confirmation page!). Still, most big vendors will provide some good analytics about the amount of traffic you’re sending them and how well it’s converting. But if we can’t track the actual sale directly ourselves, can we at least track the click event? We sure can! Let’s walk through the process step by step and we’ll even go one step further and define an affiliate link click event as a Conversion event in Google Analytics. This will be especially useful as it will allow us to accurately measure how effective we at driving traffic to merchants – and keep them honest 🙂

Step 1 – Make sure you have Google Analytics installed and configured.

We’ll not go into how to do that in this post. There are plenty of excellent resources on how to do this. We recommend Google Analytics for WordPress by MonsterInsights.

Step 2 – Add some custom Javascript to track external/affiliate product links in WooCommerce

There’s a ton of ways to do this but the easiest way is to install the Code Snippets plugin and then add the following as a new snippet.

In summary, the script above will:

  • Look for any instances of a p element on your site with the css class cart – which is the default markup WooCommerce uses for the add to cart button

  • Next, our script will check to ensure that Google Analytics is installed and that the url is indeed an external link.
  • We then create a custom GA event where we’ve defined a few important values.
    • Event Category – you can call this whatever you want – I’ve called it External Affiliate Link
    • Event Action – this is the name for the actual event that has taken place – in this case I’ve called this clicked
    • Event Label – this is set to be the actual url that has been clicked

Step 3 – Setup your External/Affiliate product in WooCommerce

This might be stating the obvious but this script will only work on External/Affiliate product types so you’ll need at least one of those 🙂

Ensure you add your external product link to your External/Affiliate product in WooCommerce
Ensure you add your external product link to your External/Affiliate product in WooCommerce


Step 4 – Confirm your event is working correctly in Google Analytics

Open your External/Affiliate product in WooCommerce and click the external link. If you’ve followed all our steps so far this will be sending events to Google Analytics for you. Go ahead and the click the button a few times to trigger a few test events. Then, hop on over to Google Analytics and go to Real Time -> Events (Note: It’s important to confirm your test works via Real Time as Events don’t show up in Behaviour -> Events for a few hours).

You should see something like this.

Our affiliate link tracking event in Google Analytics
Our affiliate link tracking event in Google Analytics

Woo hoo it works! Just like that any time one of your users clicks on external product links from your catalog they will now appear in their own Event category in GA.

Bonus round – let’s track link clicks to External Affiliate links as Conversion events.

How to track affiliate link clicks as conversion events

Step 1 – Go to GA -> Admin



Step 2 – Click Goals

Step 3 – Create a new goal 


You will want to give your goal a name that makes sense to you. Then select Event as the goal type.


Step 4 – Configure your goal

Make sure to enter the exact same name for the Category and Action that we defined earlier. Label and Value can be left blank. You will see an option to check this goal against your traffic for the past week. As we’ve just defined this event don’t be surprised if you see that the goal has a 0% conversion rate even though we’ve just ran some test events. That’s just our good ‘ol data lag in GA rearing it’s head again. Hit Save.


Check that your Conversions are working.

Thankfully we can verify our conversions are working by heading back over to Real Time -> Conversions. You can see below that our external link clicks are now showing up as conversions!

And that’s it – you’re done!

Wrapping things up

Given how many people many of us rely on affiliate marketing as a source of income online, it’s vital that we make sure we know as much as possible about the amount of traffic we generate for merchant we choose to promote. As a result of implementing the tracking processes we’ve detailed here, you’ll now be in a much better position to understand how effective your promotional efforts are. Of course we’ve only scratched the surface of what you can do with Events in Google Analytics. This same approach could be deployed to other external links across your site with a few tweaks. There are also some excellent plugins available to allow you to do that without needing to get your hands dirty with Javascript such as WP Google Analytics Events and Gravitate Event Tracking which are both awesome.

New extra bonus script added!

James Winsoar over on the Advanced WooCommerce Facebook Group wanted to use this conversion tracking method with Google Adwords. Here’s an alternative code snippet to do exactly that 🙂 Just be sure to change the Adwords variables to match your account settings.

Psst! Like WooCommerce Themes? Why not check out our hand curated mega list of the very best free WooCommerce Themes.


Follow us on Twitter and Facebook!

Stay connected – be the first to get interesting updates and new releases from Create and Code.

Colm has been building web stuff since the web was a wee lad. Colm built his first blog with WordPress around 2007 and has been hooked ever since. When not knee deep in hooks and filters you'll probably find him running around a mountain or making pizza.


    1. Hi Frank. no idea to be honest – depends on how the cloak url plugin works – if it’s javascript based and is manipulating standard WooCommerce links then it could be causing it to not work.

    2. Hey Frank,

      If you use Google Tag Manager and set the link target as a label you should be fine as wel.
      You’ll get the cloaked link as an event label 🙂

      ~ Cheers

  1. Hi – great article. Is there a way to add the Member/User ID to the tracking (note: I’m using the woocommerce subscription/membership plugin) and would like to track each of the members that click on the link. Even better would be able to add variables/parameters to the URL link in the external product (one being the userID).

    Thanks in advance

  2. Hello, this script does not work with the latest version of woocommerce, because the html tags have changed.

    $(“p.cart”).on(“click”, function(e) { —-> $(“form.cart”).on(“click”, function(e) {
    var link = $(“a”); —–> var link = $(“button”);

    I tried as I described above. In Google Analytics the event is tracked, but on the e-commerce I get the 404 page not found error.

    Can someone improve / update this scripts?

    Thanks and congratulations for the article

  3. hi,
    maybe something changed since this article was written but, although the send works an I see events coming in, I noticed the Event Category in the reports (real time > events) is not what I send with js but a generic “outbound-link” which I guess causes the GOAL part of the article to not work either (strangely it doesn’t work even if I set that ‘outbound-link’ as the category for the goal)

    1. After investigation I think it’s the Google Analytics for WordPress by MonsterInsights plugin. It has a “Enable MonsterInsights events tracking:” option in the settings (tracking > engagement) which needs to be enabled for the tracking to work but apparently messes up with the data sent. I leave it to you gurus to try and find a solution 🙂

  4. We have a client who has a directory of products (let’s call it The Red Shop) that are sold by suppliers who she has been asked to feature on her website. She will add the details of each product on her site with a Woocommerce Affiliate link to the supplier’s website. Does the above method track someone who has successfully bought the product on the supplier’s store?

    ie she is not the affiliate as such, she is the directory of other people’s website stores.

    Great post, thanks!


  5. Hi, I use Google Analytics 4 and my adaption is not working. But maybe there are multiple problems at hand. For example, Wzone does the link cloaking with URL redirects, so GA4 has no way of getting the information of the redirect?
    Anyhow, soon tag based tracking will be dead and FLEDGE and FLOC might help.

  6. Hi, if we have some partners that are OK to integrate our tracking pixel/conversion code, do you know if there is a plugin that will generate a pixel code to track the sales on woocommerce reports ?

Leave a Reply

Your email address will not be published. Required fields are marked *