Introducing Ad Injector - Insert Custom Ads in Your WordPress Blog

If you run an e-commerce website, setting up Google Analytics E-Commerce Tracking is something you should do sooner rather than later. E-Commerce Tracking will provide you with valuable insights on where your sales are coming from, such as which referrers and pages are generating the most revenue.

In this post, we’ll take a look at how to set up Google Analytics E-Commerce Tracking in Easy Digital Downloads. Since EDD doesn’t have an official add-on for enabling this feature, we set out to enable it on our own website with some custom code. Here’s how we did it.

Pre-Requisites for Google Analytics E-Commerce Tracking

Before looking at the code to add Google Analytics e-commerce tracking, you need to make sure everything is in place for it to work.

First, you have to have a Google Analytics account, and the general tracking snippet installed on your website. We’ll assume you have one, so we won’t cover the basic Google Analytics integration. If you need help with this step, see this article on How to Set Up Google Analytics in 5 Simple Steps.

Assuming you now have your basic Google Analytics integration up and running, the next step is to enable E-Commerce Tracking for the property/website.

After logging in to your GA dashboard, click the “Admin” menu item (the gear icon). This will take you to a screen that looks like this.

Google Analytics admin

In the Property column, select the correct property. This is the website on which you want to enable e-commerce tracking.

Next, in the View column, select “Ecommerce Settings.” This will open up the following settings.

Google Analytics Enable E-Commerce Tracking

The “Enable Ecommerce” setting will likely be off, so toggle it on, then save your change.

The Code

Now that your website supports GA e-commerce tracking, let’s actually add the code to tell GA when it should record e-commerce data.

add_action( 'wp_footer', 'em_load_ga_ecommerce_event_tag' );
/**
 * This loads the script required for sending payment conversion to Google
 *
 * @return void
 */
function em_load_ga_ecommerce_event_tag() {

	// Bail if not on payment confirmation
	// $_GET['payment_key'] is not available on new purchases (only when later viewing receipt)
	if ( ! edd_is_success_page() || ! empty( $_GET['payment_key'] ) ) {
		return;
	}

	global $edd_receipt_args;

	$payment = get_post( $edd_receipt_args['id'] ); // Post data for payment ID
	$meta = edd_get_payment_meta( $payment->ID ); // Meta data about the transaction

	// Get some transaction data
	$payment_obj = new EDD_Payment( $payment->ID );
	$total = $payment_obj->total;
	if ( isset( $meta['cart_details'] ) ) {
		$cart_items = $meta['cart_details'];
	}

	$items = array();

	if ( ! empty( $cart_items ) ) {
		foreach ( $cart_items as $key => $product ) {
			$items[] = array(
				'id' => $product['id'],
				'name' => $product['name'],
				'sku' => edd_get_download_sku( $product['id'] ),
				'price' => $product['item_price'],
				'quantity' => $product['quantity']
			);
		}
	}

	?>
	<script>
		gtag('event', 'purchase', {
		  "transaction_id": "<?php echo $payment->ID; ?>",
		  "affiliation": "<?php bloginfo( "name" ); ?>",
		  "value": <?php echo $total; ?>,
		  "currency": "USD",
		  "tax": 0,
		  "shipping": 0,
		  "items": <?php echo json_encode( $items ); ?>
		});
	</script>
	<?php
}

You can add this code snippet to your theme’s `functions.php`. Let’s review what all this does.

  1. The first check is that the user is on the purchase confirmation page, but only if it’s a new purchase. Since EDD uses the same page for both new purchases and viewing the receipt of previous purchases, this check helps avoid duplicate recordings in the event that a customer reviews the payment receipt later on.
  2. Next, it loops through all of the products in the payment, and assembles data to send to Google Analytics. This is how GA knows which products are being sold.
  3. Finally, it loads a bit of Javascript to send a purchase event to Google Analytics. We load this in the footer to ensure that the Google tag manager script is loaded (that should be loaded in the header). We tell GA about the payment, including its total, transaction/payment ID, our currency, and the payment’s items.

Now, when a customer completes payment and is sent to the purchase confirmation page, the Google Analytics tag manager will send the purchase data for recording in your GA account.

0 Comments

Leave a Reply

Your Cart
Your cart is currently empty.
Open Cart

How do you build WordPress websites?

Are you into using page builders to create websites and earn income? That's what we love doing, too! That's why we build extensions for the most popular WordPress page builders - to help you build awesome websites and make more money.

If you enjoy the same things, we'd love to have you as part of the Elegant Modules community. Let's get to know each other!

Interests

Welcome! We're so excited to get to meet you, and look forward to chatting with you soon.