Menu
  • Our Services
      • megamenu-dev-iconDevelopment
        • Migrations
          • BigCommerce Migration
          • Shopify Migration
          • Magento Migration
          • WooCommerce Migration
          • All Migration
        • eCommerce Development
          • Custom Functionalities
          • Conversion Rate Optimization
          • Software Development
          • Connectors
        • Ecommerce Platforms
          • Bigcommerce Development
          • Shopify Development
          • Magento Development
          • Volusion Development
          • Woocommerce Development
      • megamenu-marketing-iconDigital Marketing
        • SEO
          • eCommerce SEO
          • BigCommerce SEO
          • Shopify SEO
          • Lead Generation SEO
          • SEO by Platform
        • Marketing by Industry
          • eCommerce
          • Education
          • Industrial
          • Health Care
          • Technology
          • Legal Services
        • Services
          • eCommerce PPC
          • Email Marketing
          • eCommerce Video Production
        • mockupArlyn Scales Case Study
        • Plant The Future ResponsivePlant the Future Case Study
    • Close
  • Our Work
    • Clients
      • BigCommerce Clients
      • Shopify Clients
      • Magento Clients
      • All Clients
    • Case Studies
      • Marketing Case Studies
      • Development Case Studies
      • See All
    • Our Products
      • FFL API
      • Mailem
      • Etracker Pro
      • Custom Apps on BC
    • mockupArlyn Scales Case Study
    • Close
  • About Us
    • Company
      • Team
      • Partners
    • Resources
      • Blog
      • Surge Sessions
      • Youtube
    • Join Us
      • Work at Optimum7
      • Partner with Optimum7
    • Close
888-643-0464
Speak to an Expert
Menu
  • Our Services
      • megamenu-dev-iconDevelopment
        • Migrations
          • BigCommerce Migration
          • Shopify Migration
          • Magento Migration
          • WooCommerce Migration
          • All Migration
        • eCommerce Development
          • Custom Functionalities
          • Conversion Rate Optimization
          • Software Development
          • Connectors
        • Ecommerce Platforms
          • Bigcommerce Development
          • Shopify Development
          • Magento Development
          • Volusion Development
          • Woocommerce Development
      • megamenu-marketing-iconDigital Marketing
        • SEO
          • eCommerce SEO
          • BigCommerce SEO
          • Shopify SEO
          • Lead Generation SEO
          • SEO by Platform
        • Marketing by Industry
          • eCommerce
          • Education
          • Industrial
          • Health Care
          • Technology
          • Legal Services
        • Services
          • eCommerce PPC
          • Email Marketing
          • eCommerce Video Production
        • mockupArlyn Scales Case Study
        • Plant The Future ResponsivePlant the Future Case Study
    • Close
  • Our Work
    • Clients
      • BigCommerce Clients
      • Shopify Clients
      • Magento Clients
      • All Clients
    • Case Studies
      • Marketing Case Studies
      • Development Case Studies
      • See All
    • Our Products
      • FFL API
      • Mailem
      • Etracker Pro
      • Custom Apps on BC
    • mockupArlyn Scales Case Study
    • Close
  • About Us
    • Company
      • Team
      • Partners
    • Resources
      • Blog
      • Surge Sessions
      • Youtube
    • Join Us
      • Work at Optimum7
      • Partner with Optimum7
    • Close

888-643-0464

Speak to an Expert

Menu
  • Our Services
      • megamenu-dev-iconDevelopment
        • Migrations
          • BigCommerce Migration
          • Shopify Migration
          • Magento Migration
          • WooCommerce Migration
          • All Migration
        • eCommerce Development
          • Custom Functionalities
          • Conversion Rate Optimization
          • Software Development
          • Connectors
        • Ecommerce Platforms
          • Bigcommerce Development
          • Shopify Development
          • Magento Development
          • Volusion Development
          • Woocommerce Development
      • megamenu-marketing-iconDigital Marketing
        • SEO
          • eCommerce SEO
          • BigCommerce SEO
          • Shopify SEO
          • Lead Generation SEO
          • SEO by Platform
        • Marketing by Industry
          • eCommerce
          • Education
          • Industrial
          • Health Care
          • Technology
          • Legal Services
        • Services
          • eCommerce PPC
          • Email Marketing
          • eCommerce Video Production
        • mockupArlyn Scales Case Study
        • Plant The Future ResponsivePlant the Future Case Study
    • Close
  • Our Work
    • Clients
      • BigCommerce Clients
      • Shopify Clients
      • Magento Clients
      • All Clients
    • Case Studies
      • Marketing Case Studies
      • Development Case Studies
      • See All
    • Our Products
      • FFL API
      • Mailem
      • Etracker Pro
      • Custom Apps on BC
    • mockupArlyn Scales Case Study
    • Close
  • About Us
    • Company
      • Team
      • Partners
    • Resources
      • Blog
      • Surge Sessions
      • Youtube
    • Join Us
      • Work at Optimum7
      • Partner with Optimum7
    • Close

888-643-0464

Speak to an Expert

Back to Article List
3 minute read

Dynamic Google Analytics Event Tracking for WordPress

  • In Web Development, Wordpress
  • August 19, 2011

What Is Analytics Event Tracking?

Event tracking is a method made available by Google Analytics using the ga.js tracking code. It is used to track custom user activity with UI elements, known as “Events”. With Event Tracking, the total number of pageviews is never skewed–as is the case when using urchin.js to track user activity. Event Tracking provides for hierarchical organization of events into Categories, Actions, and Labels.

Though Event Tracking can be used in as many ways as you can imagine, this article focuses on dynamically tracking outbound links on a WordPress site.

Dynamic Outbound Tracking in Action

Follow these simple steps to automatically track all of your outbound links in Google Analytics. Make sure your Analytics code is already installed. This can be accomplished by adding the code to your header.php file right before the closing head element. Alternatively, if you don’t want to mess with your template files, there are plenty of plugins available to get the job done such as Google Analytics for WordPress.

Step 1

Create a new file called ‘event-tracking.js’ and place it in your theme’s js folder. Add the following code:

jQuery(document).ready( function($) {

	//set our domain as a variable
	var domainString = document.domain;
	
        //select each anchor whose href begins "http://"
        //this excludes relative urls and urls like "mailto:"
	$('a[href^="http://"]').each(function() {
	
		//set url's href as a variable
		var url = $(this).attr('href');
		
		//if url does not contain our domain name
		if ( url.indexOf( domainString ) == -1 ) {
			
                        //add onclick tracking event
			$( this ).attr( 'onClick', '_gaq.push(["_trackEvent","Outbound Links","Click","'+url+'"])' );
		
		} 
	});
});

Instead of using “.each( function() {} )”, you can use “.click( function() {} )” and fire the onClick event directly rather than append the onClick attribute in your html. In this example, I have appended the onClick event to make troubleshooting easy.

Step 2

Include the script in the head of each page/post by adding this function to your theme’s ‘functions.php’ file:

//if in front end (not admin)
if ( ! is_admin() ) { 

	//register our script with handle 'event_tracking'
	wp_register_script( 'event_tracking', get_bloginfo('template_directory') . '/js/event-tracking.js', array('jquery'), '1.0' );

	// enqueue the script
	wp_enqueue_script('event_tracking');

}

Note: If you’re using a child theme like Thematic, use ‘stylesheet_directory’ in place of ‘template_directory’

Finding the Results

In your Analytics account, navigate to Content>Event Tracking. You can click through the Categories and Actions to view all Labels tracked under each.

Feel free to explore the many different views available. To add a secondary dimension, like which page the event fired from, click the drop-down box next to the primary dimension (in the picture above, its value reads “None”).

Extending & Modifying the Code

The above code is just a small example of what you can do with Event Tracking. This code can be easily modified to suit your needs. For example, to assign different event categories to group outbound links in the header, body, and footer areas of your site, you might select anchor elements by class and apply your onClick events accordingly.

Event tracking is certainly not limited to outbound links. It can be used for tracking interactive events such as clicking the Play, Stop, and Pause buttons of a movie, or for tracking file downloads. A quick look at Google’s Event Tracking Guide should give you a few more ideas.

What have you used Analytics Event Tracking for in your projects?

Share on social

Related Articles

In Client Management
4 minute

8 Tips to Make Design for International Clients a Breeze

Read More
In Online Content
4 minute

How To Keep Your Customers Coming Back

Read More
eCommerce SEO Audits: How to Audit Your Website’s Content Strategy
In Surge Session
5 minute

eCommerce SEO Audits: How to Audit Your E-Commerce Site's Content Strategy

Read More

Let's Talk

A digital marketing strategy is the path to profitability. Optimum7 can help you set the right goals, offer and implement creative and technical strategies, and use data and analytics to review and improve your business’s performance.

Speak to an Expert

Stay in touch with us.

/optimum7marketing

/optimum7

/optimum7

/Optimum7

For next generation E-commerce stores.
© Optimum7.com 2022, All Rights Reserved.
888 643 0464
Mail us now
Ecommerce
  • E-commerce Migration
  • Custom Functionalities
  • E-commerce Platforms
  • E-commerce CRO
Digital Marketing
  • E-commerce SEO
  • Bigcommerce SEO
  • Shopify SEO
  • Paid Search
Development
  • Bigcommerce Development
  • Shopify Development
  • Wordpress Development
  • Software Development
About Us
  • Team
  • Media
  • Blog
  • Case Studies
  • Industries We Serve
  • BLOG
  • OUR SERVICES
  • PARTNERS
  • MEDIA
  • WORK WITH US
  • POLICIES
  • CONTACT US
Share on Social Media