How to track comments in WordPress as GOALS using Google Analytics

It’s important for a non-ecommerce site, such as a blog, to keep an eye on efficiency, how well it perform. One measure we can implement, one KPI is the number of comments. I think is important for o blogger to write as good as possible and try to have a lot of trackbacks or comments to that posts. So, I decided to try to measure this number of comments and puting in GA as a goal to my blog.

The GOALS can be implemented very easy in GA using 2 different URLs: one for the comment form (example-blog.com/write-a-comment.html) and the second one the url for thanking that the user has posted a comment (example-blog.com/thaks-you-for-comment.html). That is easy. But the WordPress has some other structure of capturing and posting a comment. Could be the Comment Goal deployed by using Regular Expressions but seems to me quite complicated, and not as good as this method: altering the GA tracking code and using some variable as comment-posted=true.

Step1: inserting a variable in the URL for tracking when a comment was posted

In wp-comments-post.php, which should be located in root, at line 72 (I have an old version of wordpress, so keep looking for redirecting which should be at the end of that file) we have the redirect to the same URL as the post after a comment is posted:

$location = ( empty($_POST[‘redirect_to’]) ? get_permalink($comment_post_ID) : $_POST[‘redirect_to’] ) . ‘#comment-‘ . $comment_id;

you shoud alter that URL and insert some variable, in my case comment=1:

$location = ( empty($_POST[‘redirect_to’]) ? get_permalink($comment_post_ID) : $_POST[‘redirect_to’] ) . ‘?comment=1’.’#comment-‘ . $comment_id;

Update: ERRATA 1 – in the new WP version I didn’t succeed to pass a variable using the URL, a get variable (there is a function called apply_filters() which strip my URL and wipe the string ?comment=1 from it). So I made some workaround using a cookie. So, for the 2.7 version of WordPress users put this line before the redirect took place:

setcookie(‘comment_posted’, ‘1’, time() + 40, COOKIEPATH, COOKIE_DOMAIN);

 

Step 2: Altering the tracking code for Google Analytics

Google Analytics has a function which track, or tag a page, it’s the same. The usual code for doing this is:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src=’" + gaJsHost + "google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXX-X");
pageTracker._trackPageview();
} catch(err) {}</script>

and now you whish to capture that variable, COMMENT=1, which means you have a comment posted:
(the URL after a comment is made at this post should look like this http://www.liviutaloi.ro/2009/02/13/ how-to-track-comments-as-goals-in- wordpress-using-google-analytics / ?comment=1 # comment-1011)

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src=’" + gaJsHost + "google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXX-X");
<?php
$comment=$_REQUEST[‘comment’];
// you look for that variable in URL, a GET variable
if($comment==1) {                                                                                                      
// see if it has the correct value of 1, means a comment just posted
?>
pageTracker._trackPageview("/thanks-for-comment.html");
// and then alter the URL that Google will record for this page, instead of the real URL
<?php } else { ?>
pageTracker._trackPageview();
// else, if it’s a normal URL, someone looking at this post, will not alter the URL recorded by Google Analytics
<?php } ?>
} catch(err) {}</script>

Update: ERRATA 2 – now, let’s read that cookie (this cookie will expire in 40 seconds, you could try to reduce that time, means that a user who post a comment with a frequency of 2 comments per minute it will record 1 Goal into GA – I put 40 seconds just to be sure that there is enough time for the browser to make that redirect, for slow connections)

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src=’" + gaJsHost + "google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXX-X");
<?php
$commentposted=$_COOKIE["comment_posted"];
// you look for that variable in COOKIE
if($commentposted==1) {  
// see if it has the correct value of 1, means a comment just posted
?>
pageTracker._trackPageview("/thanks-for-comment.html");
// and then alter the URL that Google will record for this page, instead of the real URL
<?php } else { ?>
pageTracker._trackPageview();
// else, if it’s a normal URL, someone looking at this post, will not alter the URL recorded by Google Analytics
<?php } ?>
} catch(err) {}</script>

After 60 seconds that cookie will expire so, if the user post another comment we can track it as a Goal. Hope this version will run for all of you.

Step 3. Define your Goal into Google Analytics

Put as Goal URL: /thanks-for-comment.html, or whatever you defined as thank you page for a comment posted, give this Goal a name and that it’s all.

 

Share Button
No tags for this post.

Related posts

13 thoughts on “How to track comments in WordPress as GOALS using Google Analytics

  1. Great post thanks Liviu.

    Here’s a problem I’m helping someone with, perhaps you could help? They have links out of their blog to external websites which they wish to track in WordPress. The links use the ‘GoCodes’ plugin which allows them to create the destination URL which could for example be http://www.XYZ.com but instead the ‘GoCodes’ plugin creates http://wordpresswebsite.com/go/XYZ

    Then in their robots.txt file there is an exclude against /go/* so that robots do not follow the links. However we cannot thus far work out how to track those clicks? Thanks in advance, Depesh
    p.s. I got this link from Avinash via Twitter – wonder if that comes up in your referrer list?!

  2. @Depesh Mandalia – Thanks a lot. It matters to me. I would recommend to use a normal a href tag but with rel=”nofollow” attribute, that instruct Google to not follow that link. And, to track outgoing links you can use events, see the script here – Google Analytics: script to track outbound links and downloads. Send me an email if you have trouble installing this. Yep.. I saw the tweet from @Avinash as referral, and I thanked him by twitter.

    @krishna – Thx.

  3. Hi,

    Somehow the redirect doesn’t work in my wordpress 2.7.

    Here’s what I altered in the comment-post.php:

    $comment = get_comment($comment_id);
    if ( !$user->ID ) {
    setcookie(‘comment_author_’ . COOKIEHASH, $comment->comment_author, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
    setcookie(‘comment_author_email_’ . COOKIEHASH, $comment->comment_author_email, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
    setcookie(‘comment_author_url_’ . COOKIEHASH, clean_url($comment->comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
    setcookie(‘comment_posted’, ‘1’, time() + 40, COOKIEPATH, COOKIE_DOMAIN);
    }

    $location = empty($_POST[‘redirect_to’]) ? get_comment_link($comment_id) : $_POST[‘redirect_to’] . ‘?comment=1’.’#comment-‘ . $comment_id;

    The result is this: myurl/comment-page-1/#comment-16

    But it should be something like: myurl/comment=1/comment-page-1/#comment-16 right?

    Thanks, Peter

  4. Pingback: Usare al meglio Google Analytics su Wordpress · Google Analytics in 30 secondi

  5. Wouldn’t be easier to just use Google Analytics regular expressions url match and do a funnel.
    Like considering a post is submited when you have something like .\/#comment\-[0-9]{1,} ?
    You could also consider Step1 the link .*\/#respond which is not mandatory.

  6. it may be easier to just add onclick=”pageTracker._trackPageview(“/thanks-for-comment.html”)” to the submit comment button.
    the technique is explained in google analytics track outbound links.

  7. Pingback: Online pe paine | Marius Sescu

  8. Foarte interesant ca si concept de a cuantifica calitatea unui blog prin cantitatea comentariilor si implicit tracking lor.
    Chiar m-a ajutat acest post, cautam acel “one thing” intr-un blog, proiect de publishing pentru a genera cat mai multe “one thing-uri” si oscilam intre adaugare la lista de emailuri, submit to rss…

    Super, merci Liviu.
    (ah, scuze pentru Romana)

  9. Pingback: Cum sa pui GOAL pe comentarii in wordpress + google analytics | andreimarinescu.ro

Leave a Reply

Your email address will not be published. Required fields are marked *