Adding Google Analytics to Wordpress' next_post_link function
Try escaping your quotes. Since you're using single quotes on str_replace()
you have to escape the quotes in the function itself.
$link = str_replace('" rel="next">', '" onclick="ga(\'send\', \'event\', \'NavNext\', \'click\');" rel="next">', $link);