For a while now I’ve been seeing strange links appearing on my blog pages. They first appeared after upgrading Google Analytics by Yoast from version 4.3.5 to version 5. I had hoped that the problem would have been fixed by now. But I’m still getting it with version 5.1.2.
Problem Summary
Expected result
The continue reading link should appear as Continue reading →.
Actual result
The link is displayed incorrectly as →’]);” class=”more-link”>Continue reading →
Workaround
The problem occurs after upgrading to Google Analytics v5.0.x
It can be fixed by taking the following actions:
- From the Plugins page select the Google Analytics by Yoast
Settings
link - Select the
Advanced
tab - Type a space into the Set path for internal links to track as outbound links: input field
- Click on the
Save changes
button.
Explanation
The problem is due to the incorrect addition of some Google Analytics tracking of an “outbound-article-int”.
The original HTML for the Continue reading link
<a href="http://oik-plugins.uk/oik-plugins/wordpress-core/#more-15739"
class="more-link">
Continue reading <span class="meta-nav">→</span>
</a>
has been modified by Yoast’s Google Analytics plugin to include some tracking of an “outbound-article-int” event.
<a href="http://oik-plugins.uk/oik-plugins/wordpress-core/#more-15739"
onclick="_gaq.push(['_trackEvent', 'outbound-article-int', 'http://oik-plugins.uk/oik-plugins/wordpress-core/#more-15739',
'Continue reading <span class="meta-nav">→</span>']);"
class="more-link">
Continue reading
<span class="meta-nav">→</span>
</a>
Additionally the embedded span around the right arrow has caused a parsing formatting error.
Proposed fix
There are two parts to the fix:
- Prevent the code from creating the
outbound-article-int
link - Ensure that the
Continue reading
link text is correctly escaped; we certainly don’t want the double quotes.
Solution to prevent the code from creating the outbound-article-int
A quick fix is to check on the value of $out before performing the strpos() in Yoast_GA_Frontend::get_target()
$out_links = explode( ',', $this->options['track_internal_as_outbound'] );
if ( count( $out_links ) >= 1 ) {
foreach ( $out_links as $out ) {
if ( ! empty( $original_url ) && ! empty( $domain['domain'] ) ) {
if ( $out && strpos( $original_url, $domain['domain'] . $out ) !== false ) {
$type = 'internal-as-outbound';
}
}
}
}
A better fix would also move the !empty() tests out of the loop, before the explode().
Note:Initially I couldn’t reproduce the problem in my development environment since my host name is “qw”… so the $domain[‘domain’] is empty. I hacked the code to be able to create the problem.
Solution for the Continue reading link
Further investigation indicates that this problem is associated with themes generated using Artisteer. The Google Analytics for WordPress code correctly attempts to escape the link text
esc_attr( strip_tags( $link['link_text'] );
but this doesn’t achieve anything when the link text is %%theme_more%%
which gets replaced later on in the processing.
It would appear that the fix would need to be applied to the theme. The $more_tag is defined in theme_get_excerpt() in functions.php. This uses double quotes in the span around the right arrow. Change these to escaped single quotes and the problem goes away.
function theme_get_excerpt($args = array()) {
global $post;
$more_tag = theme_get_array_value($args, 'more_tag', __('Continue reading <span class=\'meta-nav\'>→</span>', THEME_NS));
If the first fix has been applied then this change is only necessary if “outbound-article-int” processing is required and it’s a read more link.
See also
It turns out I was not the only person with these problems.