How to track Drupal comments as goals in Google Analytics

I was inspired by this episode of SEOMoz's whiteboard friday where Rand talks about tracking non-traditional conversions (like when someone posts a comment on your blog) so I decided to check out how easy this was to do in Drupal.

By default, when someone submits a comment, they are redirected back to the node to view their comment (even if it needs approval first). This causes a problem because it doesn't really give us a unique URL to use as a goal in Google Analytics (unlike the webform module).

What you'll need to do is grab the great actions module which we will use to redirect the user to a custom thank you page once their comment has been submitted. And while thats downloading and installing, create a new page that will be the thank you page when users submit a comment.

Next, navigate to Site Configuration > Actions and create a new advanced action to redirect to your new thank you page click save. Now, just head over to the Triggers settings (Site Building > Triggers > Comments) and assign your new action to the "After saving a new comment" trigger.

Make a new advanced action available

Now all you need to do is setup the goal in Google Analytics and wait for the data.

Do you have any other non-traditional conversions that you're tracking? If so, what are they and how are you tracking them?

Comments

Nice tip.. not using it atm, but it's a good usage of actions module

It would be nice to keep the user experience while still collecting the data. Lets muse on this a bit more.

I've just had a thought of how this could be done by adding an extra line to the Google Analytics code that will allow us to track the goal, while still redirecting the user back to their comment. I'll play around with that this weekend and let you know how I go.

This is a great idea- I've been trying to think of a way of doing this for ages. One question though- is it possible to redirect users back to the where they came from after they've submitted their comment? So in other words, they view the thank you page and are then redirected back a few seconds later on the 'normal' path to view their posted thread?

Emily @ dir

;-) subscribed

Great idea. If you find a way to do this then I'll use it on my site.

On a related point I'm trying to find a way to get an action to send me an email with the text of the comment. The "%body" variable sends the body of the original post rather than the comment.

I've finally had a chance to have a look at this. Initially I thought it could be done by using the the _trackPageview function to override the path tracked by Google Analytics but it wouldn't be a very simple solution. I'll keep an eye out for a better solution, but at this stage I can't think of an equally simple solution that will retain the same level of user experience.

This is a great idea- I've been trying to think of a way of doing this for ages. One question though- is it possible to redirect users back to the where they came from after they've submitted their comment? So in other words, they view the thank you page and are then redirected back a few seconds later on the 'normal' path to view their posted thread?

This is great since I was looking for this answer. I have been using Analytics for some time and haven't been able to figure out how to provide a unique url to track goals with.

Generally you can also pass in a value to Google Analytics

pageTracker._trackPageview();

So if it was a user registration page and your url was www.example.com/user/registration

once the user registers they are taken to the page:
www.example.com (home page) but with a message that says thanks for registering.

Is there a way to modify the analytics code so that you can pass in pageTracker._trackPageview(user/registration/complete);

I'll look at the Actions Module, although I thought this was already in Drupal 5? Or is it 6 I'm thinking of.

Did you manage to resolve this and make it work? It would be very useful if its possible!

Great tip! Thanks. I hope this works for tracking new user registrations as well. Will certainly look much better with a "Thank you for registering, here's what you can do next" page instead of sending new users to their account page, baffled at what to do next.

Using the Actions module and the confirmation page is a nice workaround. I still wish there was a better way to do this, where the workflow wouldn't be broken.

Thanks for the tip.

Did you manage to resolve this and make it work? It would be very useful if its possible!

If you want to do it seamlessly simply edit your module and template accordingly taking advantage of _trackPageview().

Here is an example of how I capture registrations on www.deangraziosi.com.

In user.module I add the following two lines to user_register_submit().

global $googlegoal;

and then at the bottom of the function before the "return" (when a successful reg is made) I add:

$googlegoal = 'register';

IE:

function user_register_submit($form_id, $form_values) {
global $base_url, $googlegoal;
...
else if (!variable_get('user_email_verification', TRUE) && $account->status && !$admin) {
// No e-mail verification is required, create new user account, and login user immediately.
$subject = _user_mail_text('welcome_subject', $variables);
$body = _user_mail_text('welcome_body', $variables);
drupal_mail('user-register-welcome', $mail, $subject, $body, $from);
user_authenticate($account->name, trim($pass));
$edit = array();
user_module_invoke('login', $edit, $account);
$googlegoal = 'register';
return '';
}
}

Then in your theme open page.tpl.php add this right above the tag:


<?
switch ($googlegoal) {
case 'register':
print '
var pageTracker = _gat._getTracker("UA-2701762-1");
pageTracker._trackPageview("/registered.html");
';
}
?>

You can add more goals here to the switch statement.

Just make sure the URL entered for your registration goal in google's tracker is: /registered.html

Oh great tips. I was trying to look up relevant articles about my problems regarding tracking. This really a great help. Thanks Schoonzie!

I called it everywhere. Thanks

There is a need for better traffic analysis in Drupal. A possible implementation of this project may involve writing a script separate from Drupal that accesses server logs directly. Other features may include an API to allow contrib modules to log events or a way to send statistics to third party tools are hugely used by seo services firms, such as Google Analytics.

I have never thought about using analytics to track the comments on my Drupal site. Very good stuff! I love your idea, too JB. Thanks for adding that for us.

Add new comment