Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using Disqus for my blog comments, and Google Analytics for tracking users. I have both implemented using plugins (Disqus Comment System and Google Analyticator). I'd like to track comments, but I can't figure out how to do that.

I have researched the problem, and there's some documentation for it, but it's not very clear. So far I've found some help from Disqus themselves (here), and from On Digital (here), so now I know what code to add.

What they don't mention is where to add it.

Do I insert it in one of the Disqus plugin files? My theme? Which file, and where?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You can use that snippet just anywhere where it can be rendered on single pages. For example, you can paste this under functions.php of your theme:

function hook_disqus_config(){

if( ! is_singular() ) return;

?>
<script type="text/javascript">
    function disqus_config() {
        this.callbacks.onNewComment = [function(comment) {
            _gaq.push(['_trackEvent', 'Disqus', 'New Comment', comment.id]);
        }];
    }
</script>
<?php   
}

add_action( 'wp_footer', 'hook_disqus_config' );

Disqus should do the rest.

share|improve this answer

According to your sources you want to add this javascript code to the footer of any page or post where there are disqus comments enabled. Because Disqus Comment System replaces all instances of your Comment box you can simply check if comments are enabled on the current post and toss it in there.

Try adding this to your functions.php file in your active theme's directory:

function track_disqus_comments() {
    $post = get_queried_object();
    if (isset($post) && $post->comment_status == "open") : ?>
    <script type="text/javascript">
    function disqus_config() {     this.callbacks.onNewComment = [function(comment) {       _gaq.push(['_trackEvent', 'Disqus', 'New Comment', comment.id]);     }]; }
    </script>
    <?php
} add_action('wp_footer', 'track_disqus_comments');

Keep in mind that the _gaq object needs to already be defined. Hopefully your google analytics plugin handles this for you and you won't have to worry about it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.