The api file shows various alterations that Metatag supports. See below for a simple example. However, the example will not work correctly when the site changes its frontpage path (for example). In general, it would be helpful if the API docs demonstrate how to bubble up cacheability metatag during an alter hook.

function hook_metatags_attachments_alter(array &$metatag_attachments) {
  if (\Drupal::service('path.matcher')->isFrontPage() && \Drupal::currentUser()->isAnonymous()) {
    foreach ($metatag_attachments['#attached']['html_head'] as $id => $attachment) {
      if ($attachment[1] == 'title') {
        $metatag_attachments['#attached']['html_head'][$id][0]['#attributes']['content'] = 'Front Page Title for Anonymous Users';
      }
    }
  }
}

Comments

moshe weitzman created an issue.