Skip to content

Commit

Permalink
bugfix: When not adding tracking counters, return a proper link (#166)
Browse files Browse the repository at this point in the history
Co-authored-by: Jari Turkia <[email protected]>
  • Loading branch information
HQJaTu and Jari Turkia authored Jan 18, 2025
1 parent da16610 commit 245618d
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,29 +154,40 @@ function gtag() {
!$markupDisabledConfig && !$markupDisabledPost) {
$element = $element['element'];
$eventData[$element] = preg_replace_callback(
"#<a (.*)href=(\"|')(http://|https://|)([^\"']+)(\"|')([^>]*)>#isUm",
"#<a\\s+(.*)href\\s*=\\s*[\"|'](http://|https://|)([^\"']*)[\"|']([^>]*)>#isUm",
array($this, 'analytics_tracker_callback'),
$eventData[$element]
);
}
}
return true;

default :
default:
return false;
} // end switch ($event) {
}

/**
* matches:
* 0 = entire regexp match
* 1 = anything between "<a" and "href"
* 2 = scheme
* 3 = address
* 4 = anything after "href" and ">"
*/
function analytics_tracker_callback($matches)
{
$parsed_url = parse_url($matches[3].$matches[4]);
$parsed_url = parse_url($matches[2].$matches[3]);

// Skip tracking for local URLs without scheme, or unknown scheme.
if (!isset($parsed_url["scheme"]))
return;
return $matches[0];
if (!in_array($parsed_url["scheme"], array("http", "https")))
return;
return $matches[0];

// Note: Assume, there is no second onclick-event in substr($matches[0], 2)
return '<a onclick="_gaq.push([\'_trackPageview\', \'/extlink/' .
(function_exists('serendipity_specialchars') ? serendipity_specialchars($matches[4]) : htmlspecialchars($matches[4], ENT_COMPAT, LANG_CHARSET)) .
(function_exists('serendipity_specialchars') ? serendipity_specialchars($matches[3]) : htmlspecialchars($matches[3], ENT_COMPAT, LANG_CHARSET)) .
'\']);" ' . substr($matches[0], 2);
}

Expand Down

0 comments on commit 245618d

Please sign in to comment.