add_filter: swp_analytics

  1. Home
  2. Knowledge Base
  3. Developers
  4. add_filter: swp_analytics

Not the solution you were looking for?

Click the link below to submit a support ticket. If you are a Social Warfare - Pro license holder, you can log into your Account page to submit a ticket.
Submit Ticket

Description

This filter hook modifies the inbound URL by adding analytics information to it. Social Warfare currently adds tracking data for Google Analytics, but this hook can be used to modify the URL to support tracking data from any third party analytics company that you prefer.

This filter runs immediately before add_filter: swp_link_shortening, allowing the URL to have analytics data attached to it prior to it being shortened and sent out to social media.

Example Usage

// Add your function to the filter
add_filter('swp_analytics'  , 'swp_google_analytics' );

// Create your filter function
function swp_google_analytics( $array ) {
 
    // Fetch the user options
    $options = swp_get_user_options();
    $url = $array['url'];
    $network = $array['network'];
 
    // Check if Analytics have been enabled or not
    if($options['googleAnalytics'] == true):
        $url = $url.urlencode('?utm_source='.$options['analyticsSource'].'&utm_medium='.$network.'&utm_campaign='.$options['analyticsCampaign'].'');
    
        // Return the URL or the world will explode
        return $url;
    else:

        // Return the URL or the world will explode
        return $url;
    endif;
}

Parameters

This filter makes use of WordPress default add_filter function.

$array
(array) The add_filter function will pass one parameter containing an array with the URL ($array[‘url’]) to be modified and the name of the Social Network ($array[‘network’]) the URL is being created for.

Was this article helpful?

Related Articles