• Resolved Aquif Shaikh

    (@shaikhaquif)


    Hello,

    I am currenly using Jetpack sharing, along with bunch of other promotional methods to get traffic from Social websites especially LinkedIn and Facebook.

    I would like to trace what percentage of traffic am getting through direct shares through my website. So i wanted to know how to make Jetpack sharing use my an UTM code instead of the standard URL.

    If not, is there any other way i can use to get required information as stated above?

    https://wordpress.org/plugins/jetpack/

Viewing 15 replies - 1 through 15 (of 22 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    You can use the sharing_permalink filter to customize the link that will be shared through Jetpack’s Sharing buttons. You can paste the following code in your theme’s functions.php file, or in a functionality plugin to get started:

    /**
     * Overwrite the links used in Jetpack's Sharing module.
     *
     * @see https://wordpress.org/support/topic/how-to-use-utm-codes-with-jetpack-sharing
     *
     * @param string $url
     * @param int $post_id
     * @param int $sharing_id
     * @filter sharing_permalink
     * @return string
     */
    function jeherve_custom_sharedaddy_link( $url, $post_id, $sharing_id ) {
    	$ga_params = array(
    		'utm_source'   => 'XXX',
    		'utm_medium'   => 'XXX',
    		'utm_term'     => 'XXX',
    		'utm_content'  => 'XXX',
    		'utm_campaign' => 'XXX',
    	);
    
    	return esc_url( add_query_arg( $ga_params, $url ) );
    }
    add_filter( 'sharing_permalink', 'jeherve_custom_sharedaddy_link', 10, 3 );
    Thread Starter Aquif Shaikh

    (@shaikhaquif)

    Hi Jeremy,

    Thanks a lot for the information. Will try it and let you know how it works out

    Thread Starter Aquif Shaikh

    (@shaikhaquif)

    Hi Jeremy,

    I tried the code, it did what it was supposed to. But probably that wasn’t the exact solution for me.

    The above code adds a common UTM source to all the sharing buttons. So i cannot make out if the traffic is coming from FB or Twitter or LinkedIn.

    So earlier i could know from what website am getting traffic from, but not exactly the medium and now I know what’s the medium but not the source. So does not solve the exact problem. Is it possible to add a different UTM code to each button?

    Secondly,I cannot see my Facebook Share counter since yesterday. I Stumbled upon an older thread and followed suggestions there. It seems to be working fine with official buttons. But doesn’t work with any other button type. I tried disabling all the other plugins, but that doesn’t work either, so am sure it is not related to cache or OG conflicting plugins.

    Am not sure if this is actually Bug on my side or Facebook’s side or Facebook is just moving along the lines of Twitter, to completely remove the counter feature.

    Thread Starter Aquif Shaikh

    (@shaikhaquif)

    Update: The counter issue got solved automatically. Seemed to be a temporary bug.

    Kindly ignore the second the part.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    You could use the $sharing_id to get the Social Network name, like so:

    /**
     * Overwrite the links used in Jetpack's Sharing module.
     *
     * @see https://wordpress.org/support/topic/how-to-use-utm-codes-with-jetpack-sharing
     *
     * @param string $url
     * @param int $post_id
     * @param int $sharing_id
     * @filter sharing_permalink
     * @return string
     */
    function jeherve_custom_sharedaddy_link( $url, $post_id, $sharing_id ) {
    
    	$ga_params = array(
    		'utm_source'   => $sharing_id,
    		'utm_medium'   => 'XXX',
    		'utm_term'     => 'XXX',
    		'utm_content'  => 'XXX',
    		'utm_campaign' => 'XXX',
    	);
    
    	return esc_url( add_query_arg( $ga_params, $url ) );
    }
    add_filter( 'sharing_permalink', 'jeherve_custom_sharedaddy_link', 10, 3 );

    That should do the trick.

    Thread Starter Aquif Shaikh

    (@shaikhaquif)

    Bingo!!!! Works perfectly fine.

    Thanks a lot for getting it solved that too in quick time.

    However, it would be great if you can include the UTM feature in Future updates. Or may be option to edit UTM parameters in settings. This will definitely make many more people use Jetpack sharing over other social Plugins

    Anyways, Thanks again.

    Thread Starter Aquif Shaikh

    (@shaikhaquif)

    Sorry to bother you again.

    The above code breaks Google plus link. Though Google plus is not much important for me, am sure there must be a work around.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    Could you give me an example, so I can take a closer look? What’s the link generated by the Google+ sharing button once you add that code?

    Thanks!

    Thread Starter Aquif Shaikh

    (@shaikhaquif)

    Thread Starter Aquif Shaikh

    (@shaikhaquif)

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    That’s interesting. It looks like Twitter and Google+ don’t accept URL parameters that are encoded in the URL you pass to their service.

    The only work-around I could think of would be to use a URL shortener to create a short version of your URL including the tracking parameters, and use that custom short URL in the sharing buttons.

    In the example below, I used the Goo.gl URL shortener:

    /**
     * Overwrite the links used in Jetpack's Sharing module.
     *
     * @see https://wordpress.org/support/topic/how-to-use-utm-codes-with-jetpack-sharing
     *
     * @param string $url
     * @param int $post_id
     * @param int $sharing_id
     * @filter sharing_permalink
     * @return string
     */
    function jeherve_custom_sharedaddy_link( $url, $post_id, $sharing_id ) {
    
    	// Let's see if we have a shortlink for this post
    	$tracked_url = get_post_meta( $post_id, '_tracked_googl_shortlink', true );
    
    	if ( $tracked_url ) {
    		return $tracked_url;
    	} else {
    		// Let's build our tracking parameters
    		$ga_params = array(
    			'utm_source'   => $sharing_id,
    			'utm_medium'   => 'XXX',
    			'utm_term'     => 'XXX',
    			'utm_content'  => 'XXX',
    			'utm_campaign' => 'XXX',
    		);
    
    		// Add them to our post permalink
    		$tracked_url = esc_url( add_query_arg( $ga_params, $url ) );
    
    		$tracked_url = str_replace( '&', '&', $tracked_url );
    
    		/**
    		 * Create a short Goo.gl URL redirecting to the full URL with tracking parameters.
    		 *
    		 * @see https://goo.gl/
    		 *
    		 * This uses my own API key by default.
    		 * if you want to use your own to have stats attached to your own Goo.gl account,
    		 * get your own key here: https://developers.google.com/url-shortener/v1/getting_started?hl=en
    		 */
    		$googl_key = 'AIzaSyDjjB4PmEYFAIH7NF9Q6vww7unzFk_e32s';
    
    		// Let's query Google and get a Short URL
    		$googl_result = wp_remote_post(
    			add_query_arg(
    				'key',
    				$googl_key,
    				'https://www.googleapis.com/urlshortener/v1/url'
    			),
    			array(
    				'body' => json_encode( array( 'longUrl' => esc_url_raw( $tracked_url ) ) ),
    				'headers' => array( 'Content-Type' => 'application/json' ),
    			)
    		);
    
    		// Return the default URL if the request got an error.
    		if ( is_wp_error( $googl_result ) ) {
    			return esc_url( $url );
    		}
    
    		$googl_result = json_decode( $googl_result['body'] );
    		$googl_link = $googl_result->id;
    
    		if ( $googl_link ) {
    			// Let's save that in post meta for the next call
    			add_post_meta( $post_id, '_tracked_googl_shortlink', $googl_link, true );
    
    			// And boom! return a short tracked URL.
    			return $googl_link;
    		} else {
    			return esc_url( $url );
    		}
    
    		// Final default, just in case
    		return esc_url( $url );
    	}
    }
    add_filter( 'sharing_permalink', 'jeherve_custom_sharedaddy_link', 10, 3 );

    That seems to work in my tests.

    Thread Starter Aquif Shaikh

    (@shaikhaquif)

    Hi Jeremy,
    As always you rock.

    So far that works perfectly fine. In fact that’s better than what I asked for. Now i have my long and ugly URLs hidden with goo.gl.

    Perfect!!! Thanks a lot for this one.

    Thread Starter Aquif Shaikh

    (@shaikhaquif)

    Nopes It isn’t working.

    After spending some time, this is what i found.

    Just check this link http://goo.gl/nLQWvS” and see the UTM paramaters.

    It has a lot of #38’s in it. What’s interesting is as soon as you post the longer URL with those #38, it gets converted to Normal URL. Check the link below. Its the same link

    http://www.bloggerzarena.com/?utm_source=XXX#038;utm_medium=XXaX&utm_term=XXX&utm_content=XXX&utm_campaign=XXX

    So, what happens in our case is, we are shortening the URL before posting and hence it is not properly configured to be tracked by GA and hence the traffic is being shown as direct traffic.

    What’s your take on this?

    Am sorry for too may edits, but that’s just because no matter what am trying quotes, half urls whatever, those #38’s were getting removed automatically and so the message would have been confusing.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    It has a lot of #38’s in it.

    That shouldn’t be a problem. & is an encoded & character. If you don’t like seeing an encoded character there, you could add this right before you shorten the URL.

    So, what happens in our case is, we are shortening the URL before posting and hence it is not properly configured to be tracked by GA and hence the traffic is being shown as direct traffic.

    Try editing the different UTM values so it gets properly sorted out by Google Analytics. XXX were just default values I added there, but you could use just about anything.

    Edit: turns out I have the same problem you had. The forum’s editors automatically transforms & into &, even in code blocks.

    Thread Starter Aquif Shaikh

    (@shaikhaquif)

    That shouldn’t be a problem. & is an encoded & character.

    Even I thought the same. But it is the one which turned out to be the culprit. After removing those #38’s by adding the code you provided, it is now tracking correctly.

    Try editing the different UTM values so it gets properly sorted out by Google Analytics. XXX were just default values I added there, but you could use just about anything.out anything.

    Yep I got that, but that wasn’t culprit as I said. But now it’s creating trouble, Since we are checking for shortlinks at the very beginning of the code, It seems to give out those “XXX” parameters.

    I tried removing the “checking shortlink” part from the code. It created a fresh Shortlink as expected. After that, I re-added that part of code as I though it may give out the latest created short link, but it is rather giving out the first shortlink it created. The one with XXX parameter.

    Is there any way to reset it?

    Also i wanted to know if I can include this code at my blog with due credit to you and a link back to this post?

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘How to use UTM codes with Jetpack Sharing?’ is closed to new replies.