Last active
August 29, 2015 13:56
-
-
Save AramZS/8930496 to your computer and use it in GitHub Desktop.
A quick WordPress template tag to create Google Analytics Event Tracking on links. Built on behalf of CFO Publishing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get a safe string for custom variables or event tracking for Google Analytics. | |
*/ | |
function get_for_google_analytics($string){ | |
$string = strip_tags($string); | |
$string = remove_accents( html_entity_decode($string) ); | |
$safe_string = esc_js( $string ); | |
return $safe_string; | |
} | |
/** | |
* Echo an event tracking code for Google Analytics. | |
*/ | |
function the_event_tracking($category, $action, $label, $value = 1, $noninteraction = false){ | |
if (!is_int($value)){ | |
$value = 1; | |
} | |
if (!is_bool($noninteraction)){ | |
$noninteraction = false; | |
} | |
$boolString = ($noninteraction) ? 'true' : 'false'; | |
$s = sprintf('onClick="_gaq.push([%1$s, %2$s, %3$s, %4$s, %5$s, %6$s]);"', | |
"'_trackEvent'", | |
"'" . get_for_google_analytics($category) . "'", | |
"'" . get_for_google_analytics($action) . "'", | |
"'" . get_for_google_analytics($label) . "'", | |
$value, | |
$boolString | |
); | |
echo $s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment