How to track SKU as item_id?

Go back to Documentation

By default Google Tag Manager for WooCommerce plugin tracks eCommerce events with an internal WooCommerce Product ID as the item_id that will be passed to connected 3rd party tools such as Google Analytics. This is sane default behavior as the item_id needs to be a unique string, but if you leverage SKU as your leading product identifier you can easily switch your plugin to use it instead of the default ID.

First, make sure that you have unique and reliable SKU present on all of your products in your WooCommerce shop, because items without correct id won’t be tracked correctly.

Second, paste the following code snippet in your theme functions.php:

add_filter("gtm_ecommerce_woo_item", function($item, $product) {
	$item->setItemId($product->get_sku());
	return $item;
}, 10, 2);

Save the file and from now on all your eCommerce events will be tracked with SKU instead of internal WooCommerce Product ID.

Leave a Comment