Working on setting up Google Analytics within a custom theme. I make use of the previous_post_link and next_post_link functions within my site to do some navigation pieces. Now I’m trying to add in some Google Analytics to those.
I’ve added this to my functions.php file:
add_filter('next_post_link', 'ga_next_post_link');
function ga_next_post_link($link) {
$link = str_replace('" rel="next">', '" onclick="ga('send', 'event', 'NavNext', 'click');" rel="next">', $link);
return $link;
}
add_filter('previous_post_link', 'ga_previous_post_link');
function ga_previous_post_link($link) {
$link = str_replace('" rel="last">', '" onclick="ga('send', 'event', 'NavLast', 'click');" rel="last">', $link);
return $link;
}
When I try that, I get a 500 error thrown back at me. If I replace the ga('...');
junk with test
, it will load and work fine.
Anyone know why this is, and how I can fix it?
Try escaping your quotes. Since you’re using single quotes on
str_replace()
you have to escape the quotes in the function itself.