An important goal of a blog writer is to encourage comments on your posts. It is possible to set up Google Analytics to track comment posts as a goal, which will then allow you to analyse the source of new comments etc.
To track comment posts in Google Analytics, you need to send an event when the comment posting form is submitted. All of the solutions I have seen so far require one to add an onClick to the comment form submit button. For example:
<input name="submit" type="submit" onClick="ga('send', 'event', 'Comment', 'Submit');" id="submit" value="Post Comment" />;
Note that the code above is using the Universal Analytics event tracking format. If you are using the older ga.js script, you will need something like:
<input name="submit" type="submit" onClick="_gaq.push(['_trackEvent', 'Comment', 'Submit']);" id="submit" value="Post Comment" />;
This is fine if your theme uses a comment form template, but what if it uses the built-in WordPress comment_form function to generate the form? Open up your theme’s comments.php. If you can find a line like:
<?php comment_form(); ?>
then your theme is using the standard function. While it is easy to customise the various form fields using hooks, it is not possible to add an onClick to the submit button without making changes to /wp-includes/comment-template.php. Making changes to core WP files is bad practice as these can be overwritten by WordPress updates.
The solution is to bind the Google Analytics event to the form submit using jQuery. To do this, open your theme’s comments.php located at /wp-content/themes/your-theme-name/comments.php. My theme contained the following line which displays the comment form:
<?php if ( comments_open() ) { comment_form(); } ?>
I inserted a jQuery script to track a GA event when the form submit button is clicked:
<?php if ( comments_open() ) { ?> <script> jQuery(document).ready(function($) { $('#submit').on("click",function() { ga('send', 'event', 'Comment', 'Submit'); console.log('onClick fired!'); }); }); </script> <?php comment_form(); } ?>
- For those not using Universal Analytics, replace line 5 above with:
_gaq.push(['_trackEvent', 'Comment', 'Submit']);
- To check if the event is firing correctly, go to a page with comments and open your browser developer console. Submit a test comment. You should see “onClick fired!” in the console log. Once you have confirmed it is working, you can delete line 6 above.
Hi there! Great little tip, however I’m getting the following error:
“Uncaught ReferenceError: ga is not defined”. Any ideas on what I’m missing? Obviously the function is not being defined correctly but I’m not what’s missing.
Sorry, last line meant to read “…but I’m not SURE what’s missing.” 🙂
Hi Jeremy. It sounds like you haven’t installed the Google Analytics universal tracking code, or you’ve got the old version installed. Instructions here.
Thanks for this method of getting GA to track comment posts in wordpress. What I’m wondering is how do I setup GA to track this?
Hi Ryan. Once you have added the tracking code, set up the goal in GA admin by going to My Account > Site Name > All Web Site Data > Goals and clicking Add New Goal. Follow the steps to set up the goal. When I get a chance, I’ll add step by step instructions to the post.