Metatag: How to programmaticaly set a node's canonical URL
look into the hook: metatag_metatags_view_alter().

/**
 * Implements metatag_metatags_view_alter()
 */
function mymodule_metatag_metatags_view_alter(&amp;$output, $instance) {
  // Add Custom logic to overwrite the canonical tag here.
  $canonical = 'http://www.mysourceurl.com';

  // Alter canonical tag.
  $output['canonical']['#attached']['drupal_add_html_head'][0][0]['#value'] = $canonical;
}