2

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?

2 Answers 2

1

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.

1
  • This answer isn't working for me - I was just wondering if it is still valid? The code is being added to my page after the GA scripts, and before the Disqus scripts but the function does not appear to be firing.
    – Tomtids
    Apr 21, 2016 at 13:08
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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