jetpack icon indicating copy to clipboard operation
jetpack copied to clipboard

Sharing: Filter share URL per service

Open clifgriffin opened this issue 9 years ago • 8 comments

We'd like to provide a patch to allow filtering share URLs per service. The purpose is to add a UTM tracking code to the shared URL.

Before we submit a PR, we wanted to get pre-clearance as there are a couple of ways to do it:

  1. We could add a filter to each share service (or perhaps the parent class).
  2. We could add a parameter to get_share_url() and pass in the service from each instance.

The first option feels cleanest to me. Is there a third option we are not thinking of?

clifgriffin avatar Jan 02 '17 18:01 clifgriffin

I just came across https://wordpress.org/support/topic/how-to-use-utm-codes-with-jetpack-sharing/ yesterday, which might be of interest to you.

rvdsteege avatar Jan 05 '17 07:01 rvdsteege

@rvdsteege Thanks, the issue is that we want to do a separate tracking code per service. The filter he notes would universally apply to all services.

clifgriffin avatar Jan 10 '17 22:01 clifgriffin

@jeherve If someone can comment on the best architecture, I'm willing to put together a pull request for this.

clifgriffin avatar Jan 16 '17 19:01 clifgriffin

@clifgriffin that's a very interesting problem. We have discussed it a little bit, and I think there are several ways you can achieve the desired result. As you can see in the thread, there is a way to see which sharing service the filter is being called for using the third parameter. You could do a simple string match and based on that modify the URL to your needs. Will that solve your problem?

zinigor avatar Jan 18 '17 09:01 zinigor

@zinigor Man, don't know how I missed that. I'll give it a try!

clifgriffin avatar Jan 18 '17 14:01 clifgriffin

@zinigor You were right. I should read better next time.

However I did find a crucial problem with the example used in the link above. Here's my working code:

add_filter('sharing_permalink', 'obj_share_link_utm', 10, 3);

function obj_share_link_utm($permalink, $post_id, $sharing_id) {
	$ga_params = array(
		'utm_source'   => $sharing_id,
		'utm_medium'   => 'Social',
		'utm_campaign' => 'ShareBar',
	);

	return add_query_arg( $ga_params, $permalink );
}

The example here https://wordpress.org/support/topic/how-to-use-utm-codes-with-jetpack-sharing/ used esc_url before returning the filtered permanlink, but this leads to double encoding of some characters causing Twitter to be unable to parse the URL.

clifgriffin avatar Jan 18 '17 17:01 clifgriffin

Re-opening this. The email share button bypasses get_share_url(), so it doesn't run the URL through sharing_permalink.

Instead it uses this chain:

do_action( 'sharing_email_send_post', $data );

sharing_email_send_post() runs sharing_email_send_post_content() to get the content. It passes in $data from above.

This is sharing_email_send_post_content():

function sharing_email_send_post_content( $data ) {
	/* translators: included in email when post is shared via email. First item is sender's name. Second is sender's email address. */
	$content  = sprintf( __( '%1$s (%2$s) thinks you may be interested in the following post:', 'jetpack' ), $data['name'], $data['source'] );
	$content .= "\n\n";
	$content .= $data['post']->post_title."\n";
	$content .= get_permalink( $data['post']->ID )."\n";
	return $content;
}

I was just going to patch this to send the filtered URL in $data, but there are some backwards compatibility issues to consider. I'm going to submit a PR of how I think this could work and reference this issue.

clifgriffin avatar Jan 18 '17 19:01 clifgriffin

See #6342 and #6124

jeherve avatar Feb 22 '17 21:02 jeherve