Skip to content

Commit

Permalink
feat: append utm_medium param to all links in a newsletter (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek authored Jul 13, 2020
1 parent 3e8fff2 commit e7b1f83
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion includes/class-newspack-newsletters-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,28 @@ function ( $key ) use ( &$attrs ) {
return $attrs;
}

/**
* Append UTM param to links.
*
* @param string $html input HTML.
* @return string HTML with processed links.
*/
private static function process_links( $html ) {
preg_match_all( '/href="([^"]*)"/', $html, $matches );
$href_params = $matches[0];
$urls = $matches[1];
foreach ( $urls as $index => $url ) {
$url_with_params = add_query_arg(
[
'utm_medium' => 'email',
],
$url
);
$html = str_replace( $href_params[ $index ], 'href="' . $url_with_params . '"', $html );
}
return $html;
}

/**
* Convert a Gutenberg block to an MJML component.
* MJML component will be put in an mj-column in an mj-section for consistent layout,
Expand Down Expand Up @@ -590,7 +612,7 @@ private static function post_to_mjml_components( $post, $include_ads ) {
$body .= $ads_markup;
}

return $body;
return self::process_links( $body );
}

/**
Expand Down

0 comments on commit e7b1f83

Please sign in to comment.