Skip to content

Instantly share code, notes, and snippets.

@AramZS
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AramZS/8930496 to your computer and use it in GitHub Desktop.
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.
<?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