• I want to be able to track comments but I can’t figure out what file to edit with the Google Analytics Javascript snippet. I thought it was wp-comments-post.php but it doesn’t seem to be working. Where would this snippet go and how can I use it to track the number of comments?

    Thanks.

Viewing 10 replies - 1 through 10 (of 10 total)
  • I’m not sure how I would do this. Since Google Analytics uses javascript and wordpress is in php it would probably be easier to write it as a plugin.

    I would have thought it would be best to intercept the form submit process with javascript, run the Google Analytics query, and then submit the form.

    I’m not good enough with javascript to tell you how to do this though. Sorry.

    Thread Starter tamar

    (@tamar)

    Anyone have an idea without requiring an addon plugin?

    Thread Starter tamar

    (@tamar)

    Someone? Anyone?

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    I had to look up the Goal Tracking on Google Analytics to understand what it does and how it works.

    Comments are made to wp-comments-form.php, and then it sends a redirect to the browser which sends them back to the post’s page. If you need some kind of intermediate page, you could do that with a plugin, but you’d be relying on a meta redirect or something.

    In short, without a plugin, there’s probably no way to do it. The comment posting process has no defined intermediate page.

    Thread Starter tamar

    (@tamar)

    Otto42: thanks for getting back to me. Is there no way to embed a Javascript snippet into the PHP script? That’s basically what I’m aiming for, but I don’t know how to do so effectively and I’m not very familiar with either programming language.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    The problem is the way the goal tracking works. It works based on the URL of the page containing the javascript. Basically, once the user gets to some URL, he’s met the goal.

    But WordPress doesn’t have any particular final destination URL for making a comment. You submit the comment, it saves, and you are redirected back to the page itself. There’s no single URL to define as the goal.

    As for where to put the Google Analytics javascript, you put it in header.php of your theme. It tracks on every single page you have, there’s no “goal specific” javascript.

    Thread Starter tamar

    (@tamar)

    Right. I have regular analytics there, but not so much for tracking the number of comments. It would be nice if there was a way to do this, but I guess I’m hoping for too much! Ah well, thanks for clarifying. 🙂

    Google Analytic’s urchinTracker allows you to track events on your site that do not generate a pageview.

    This looks like it should provide a means to accomplish this functionality, which I’d also like to see. I don’t, however, have the necessary familiarity with the wordpress codebase to implement it.

    Perhaps someone else can give it a try?

    Thanks!

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    In your theme’s comments.php file, you should find something like this:

    <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">

    Try changing it to something like this:
    <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform" onsubmit="urchinTracker('/wp-comments-post.php'); return true;">

    That will allow it to track when somebody submits a comment using your comment form. It will show up in Google Analytics as a pageview of /wp-comments-post.php.

    Thanks, Otto!

    I imagine, though, that this will leave me getting “comment” hits from all the spam – is there some way you can think of to integrate this with Akismet, or only to make it a hit when someone is auto-approved by virtue of already having posted a non-spam comment?

    It seems like the latter could be done somewhere in comments.php, whereas the former would have to involve tinkering with Akismet itself.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Adding goal conversion tracking for comments’ is closed to new replies.