Google Event Tracking with Advanced Custom Fields

If you need to add Google Event Tracking to some of your WYSIWYG Advanced Custom Fields, you might notice that ACF strips out the onclick values for the event tracking. This support post suggests setting up Google Tag Manager, but if you’re not able to use Google Tag Manager for any reason, here’s some jQuery you can use that will add the onclick values to your links.

First, for this to work you need to assign an id to each link you want to track. For this example, I’m going to use the id “example-link”:

<a href="yourlink" id="example-link">anchor text</a>

Once all of your to-be-tracked links have ids on them, you need to build the jQuery. Based from Greg Reindel’s code here for tracking outbound links, PDF downloads, etc., you can add this to your main WordPress WYSIWYG editor in HTML view:

<script type="text/javascript">jQuery(window).on('load', function(){ 
    jQuery('#example-link').attr("onclick","_gaq.push(['_trackEvent', 'Link Clicks', 'Name of Page or Campaign', 'Descriptive Name for your Link']);"); //repeat this for every link you want to track
 });</script>

For Google Analytics Universal:

<script type="text/javascript">jQuery(window).on('load', function(){ 
   jQuery(this).attr("onclick","ga('send', 'event', 'Link Clicks', 'Name of Page or Campaign', 'Descriptive Name for your Link']);"); //repeat this for every link you want to track
 });</script>

When you save your page, you can visit it and inspect the links you want to track. The HTML should have the Google Event Tracking onclick values in the link tags now!

0 comments on “Google Event Tracking with Advanced Custom FieldsAdd yours →

Leave a Reply