Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions languages/wp-google-analytics.pot
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ msgstr ""
msgid "Log outgoing links as events"
msgstr ""

#: wp-google-analytics.php:223
msgid "Use shortcodes to generate custom events for outgoing links"
msgstr ""

#: wp-google-analytics.php:232
msgid "Specify the shortcode as <br /><strong>[wga_event category="category name" action="action name"]</strong> some-html-with-external-links <strong>[/wga_event]</strong>."
msgstr ""

#: wp-google-analytics.php:233
msgid "You can also specify the label, value or noninteraction options for the event as <br /><strong>[wga_event category="cat name" action="act name" label="label string"]</strong> some-html <strong>[/wga_event]</strong>,<br /><strong>[wga_event category="cat name" action="act name" value="some value"]</strong> some-html <strong>[/wga_event]</strong> or<br /><strong>[wga_event category="cat name" action="act name" noninteraction="true"]</strong> some-html <strong>[/wga_event]</strong>."
msgstr ""

#: wp-google-analytics.php:227
msgid "Default"
msgstr ""
Expand Down Expand Up @@ -162,6 +174,14 @@ msgstr ""
msgid "Update Options"
msgstr ""

#: wp-google-analytics.php:618
msgid "Default category"
msgstr ""

#: wp-google-analytics.php:620
msgid "Default action"
msgstr ""

#. Plugin Name of the plugin/theme
msgid "WP Google Analytics"
msgstr ""
Expand Down
47 changes: 31 additions & 16 deletions wp-google-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@

// Adds :external for grabbing external links
$.expr[':'].external = function(obj) {
return !obj.href.match(/^mailto\:/) && !obj.href.match(/^javascript\:/) && (obj.hostname != location.hostname);
return ( wga_settings.external == 'true' ) && !obj.href.match(/^mailto\:/) && !obj.href.match(/^javascript\:/) && (obj.hostname != location.hostname);
};

// Adds :custom for custom events (they don't have to be external links!)
$.expr[':'].custom = function(obj) {
// It's a custom event if the link is inside a span.wgaevent container
return ( wga_settings.custom == 'true' ) && ( $(obj).closest('span.wgaevent').length >= 1 );

};

var send_event = function(e, href, params) {
try {
_gaq.push( [ '_trackEvent' ].concat( params ) );
/**
* If this link is not opened in a new tab or window, we need to add
* a small delay so the event can fully fire. See:
* http://support.google.com/analytics/bin/answer.py?hl=en&answer=1136920
*
* We're actually checking for modifier keys or middle-click
*/
if ( ! ( e.metaKey || e.ctrlKey || 1 == e.button ) ) {
e.preventDefault();
setTimeout('document.location = "' + href + '"', 100)
}
} catch(err) {}
}

// Document ready.
$( function() {
// Add 'external' class and _blank target to all external links
$('a:external').on( 'click.wp-google-analytics', function(e){
try {
_gaq.push( [ '_trackEvent', 'Outbound Links', e.currentTarget.host, $(this).attr('href') ] );
/**
* If this link is not opened in a new tab or window, we need to add
* a small delay so the event can fully fire. See:
* http://support.google.com/analytics/bin/answer.py?hl=en&answer=1136920
*
* We're actually checking for modifier keys or middle-click
*/
if ( ! ( e.metaKey || e.ctrlKey || 1 == e.button ) ) {
e.preventDefault();
setTimeout('document.location = "' + $(this).attr('href') + '"', 100)
}
} catch(err) {}
$('a:external:not(:custom)').on( 'click.wp-google-analytics', function(e){
send_event( e, $(this).attr('href'), [ 'Outbound Links', e.currentTarget.host, $(this).attr('href') ] );
});

$('a:custom').on( 'click.wp-google-analytics', function(e){
send_event( e, $(this).attr('href'), $(this).closest('span.wgaevent').data().wgaevent );
});
});

})( jQuery ); // Close closure.
53 changes: 51 additions & 2 deletions wp-google-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ private function __construct() {
add_action( 'get_footer', array( $this, 'insert_code' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'track_outgoing' ) );
add_filter( 'plugin_action_links', array( $this, 'add_plugin_page_links' ), 10, 2 );

// Register our shortcode (to be called later as part of the filter hook)
add_shortcode( 'wga_event', array( $this, 'do_shortcode' ) );
}

/**
Expand Down Expand Up @@ -208,13 +211,18 @@ public function field_additional_items() {
'log_404s' => __( 'Log 404 errors as events', 'wp-google-analytics' ),
'log_searches' => sprintf( __( 'Log searches as /search/{search}?referrer={referrer} (<a href="%s">deprecated</a>)', 'wp-google-analytics' ), 'http://wordpress.org/extend/plugins/wp-google-analytics/faq/' ),
'log_outgoing' => __( 'Log outgoing links as events', 'wp-google-analytics' ),
'log_shortcode' => __( 'Use shortcodes to generate custom events for outgoing links', 'wp-google-analytics' ),
);
foreach( $addtl_items as $id => $label ) {
echo '<label for="wga_' . $id . '">';
echo '<input id="wga_' . $id . '" type="checkbox" name="wga[' . $id . ']" value="true" ' . checked( 'true', $this->_get_options( $id ), false ) . ' />';
echo '&nbsp;&nbsp;' . $label;
echo '</label><br />';
}
echo '<div id="shortcodes-description" style="margin-left:25px;width:575px">';
echo '<p>' . __( 'Specify the shortcode as <br /><strong>[wga_event category="category name" action="action name"]</strong> some-html-with-external-links <strong>[/wga_event]</strong>.', 'wp-google-analytics' ) . '</p>';
echo '<p>' . __( 'You can also specify the label, value or noninteraction options for the event as <br /><strong>[wga_event category="cat name" action="act name" label="label string"]</strong> some-html <strong>[/wga_event]</strong>,<br /><strong>[wga_event category="cat name" action="act name" value="some value"]</strong> some-html <strong>[/wga_event]</strong> or<br /><strong>[wga_event category="cat name" action="act name" noninteraction="true"]</strong> some-html <strong>[/wga_event]</strong>.', 'wp-google-analytics' ) . '</p>';
echo '</div>';
}

/**
Expand Down Expand Up @@ -252,6 +260,9 @@ public function field_custom_variables() {

}

/**
* Option to ignore actions in certain areas or by certain roles.
*/
public function field_do_not_track() {
$do_not_track = array(
'ignore_admin_area' => __( 'Do not log anything in the admin area', 'wp-google-analytics' ),
Expand Down Expand Up @@ -286,6 +297,7 @@ public function sanitize_general_options( $in ) {
'log_404s',
'log_searches',
'log_outgoing',
'log_shortcode',
// Things to ignore
'ignore_admin_area',
);
Expand Down Expand Up @@ -525,8 +537,15 @@ private function _get_options( $option = null, $default = false ) {
* If we track outgoing links, this will enqueue our javascript file
*/
public function track_outgoing() {
if ( 'true' == $this->_get_options( 'log_outgoing' ) && (!defined('XMLRPC_REQUEST') || !XMLRPC_REQUEST) && ( ! is_admin() || 'false' == $this->_get_options( 'ignore_admin_area' ) ) )
wp_enqueue_script( 'wp-google-analytics', plugin_dir_url( __FILE__ ) . 'wp-google-analytics.js', array( 'jquery' ), '0.0.3' );
$log_outgoing = $this->_get_options( 'log_outgoing' );
$log_shortcode = $this->_get_options( 'log_shortcode' );
if ( ( ( 'true' == $log_outgoing ) || ( 'true' == $log_shortcode ) ) && ( !defined('XMLRPC_REQUEST') || !XMLRPC_REQUEST) && ( ! is_admin() || $this->_get_options( 'ignore_admin_area' ) == 'false') ) {
wp_enqueue_script( 'wp-google-analytics', plugin_dir_url( __FILE__ ) . 'wp-google-analytics.js', array( 'jquery' ), '0.0.4' );
wp_localize_script( 'wp-google-analytics', 'wga_settings', array(
'external' => $log_outgoing,
'custom' => $log_shortcode,
) );
}
}

/**
Expand Down Expand Up @@ -576,6 +595,36 @@ public function add_plugin_page_links( $links, $file ){
return $links;
}


/**
* Callback for shortcode filter processing
*/
public function do_shortcode( $atts, $content = '', $tag ='' ) {
if ( 'false' == $this->_get_options( 'log_shortcode' ) ) {
return $content;
}

// Extract attributes from the shortcode
$data = shortcode_atts(
array(
// Possible attributes with default values
'category' => __('Default category'), // We actually need a value for this
// (otherwise the events are not registered).
'action' => __('Default action'), // Idem here.
'label' => '', // Optional, so leave it empty.
'value' => 1, // Has to be an integer value.
'noninteraction' => false, // If ommited, the default in ga.js is false, so use that value here too.
), $atts );

// Make sure the 'value' of the event is an integer. Otherwise GA ignores the event.
$data['value'] = (int) $data['value'];

// Safe encode the whole lot.
$data = esc_attr( json_encode( array_values ( $data ) ) );

// Wrap the content with a span with a known class, so we can find it and process it from js.
return sprintf( '<span class="wgaevent" data-wgaevent="%s">%s</span>', $data, $content );
}
}

global $wp_google_analytics;
Expand Down