diff --git a/src/EntityUsageTrackBase.php b/src/EntityUsageTrackBase.php
index f7e4b10..b8dccb3 100644
--- a/src/EntityUsageTrackBase.php
+++ b/src/EntityUsageTrackBase.php
@@ -5,6 +5,7 @@ namespace Drupal\entity_usage;
 use Drupal\Core\Config\ConfigFactoryInterface;
 use Drupal\Core\Entity\ContentEntityInterface;
 use Drupal\Core\Entity\EntityFieldManagerInterface;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\Core\Plugin\PluginBase;
 use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -28,6 +29,13 @@ abstract class EntityUsageTrackBase extends PluginBase implements EntityUsageTra
    */
   protected $entityFieldManager;
 
+  /**
+   * Entity type manager service.
+   *
+   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
+   */
+  protected $entityTypeManager;
+
   /**
    * The Entity Update config.
    *
@@ -35,6 +43,20 @@ abstract class EntityUsageTrackBase extends PluginBase implements EntityUsageTra
    */
   protected $config;
 
+  /**
+   * The Drupal Path Validator service.
+   *
+   * @var \Drupal\Core\Path\PathValidatorInterface
+   */
+  protected $pathValidator;
+
+  /**
+   * The public file directory path.
+   *
+   * @var string
+   */
+  protected $publicFileDirectory;
+
   /**
    * Constructs display plugin.
    *
@@ -50,6 +72,8 @@ abstract class EntityUsageTrackBase extends PluginBase implements EntityUsageTra
    *   The EntityFieldManager service.
    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
    *   The factory for configuration objects.
+   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
+   *   The entity type manager servive.
    */
   public function __construct(
     array $configuration,
@@ -57,13 +81,15 @@ abstract class EntityUsageTrackBase extends PluginBase implements EntityUsageTra
     $plugin_definition,
     EntityUsage $usage_service,
     EntityFieldManagerInterface $entity_field_manager,
-    ConfigFactoryInterface $config_factory
+    ConfigFactoryInterface $config_factory,
+    EntityTypeManagerInterface $entity_type_manager
   ) {
     parent::__construct($configuration, $plugin_id, $plugin_definition);
     $this->configuration += $this->defaultConfiguration();
     $this->usageService = $usage_service;
     $this->entityFieldManager = $entity_field_manager;
     $this->config = $config_factory->get('entity_usage.settings');
+    $this->entityTypeManager = $entity_type_manager;
   }
 
   /**
@@ -76,7 +102,8 @@ abstract class EntityUsageTrackBase extends PluginBase implements EntityUsageTra
       $plugin_definition,
       $container->get('entity_usage.usage'),
       $container->get('entity_field.manager'),
-      $container->get('config.factory')
+      $container->get('config.factory'),
+      $container->get('entity_type.manager')
     );
   }
 
@@ -148,4 +175,103 @@ abstract class EntityUsageTrackBase extends PluginBase implements EntityUsageTra
     return $referencing_fields_on_bundle;
   }
 
+  /**
+   * Try to find an entity by the given URL.
+   *
+   * @param string $url
+   *   A valid URL string.
+   *
+   * @return \Drupal\Core\Entity\EntityInterface|null
+   *   The linked Entity object or NULL if no entity can be found for the url.
+   */
+  public function findEntityByUrl($url) {
+    try {
+      // Strip off the scheme and host, so we only get the path.
+      $site_domains = $this->config->get('site_domains') ?: [];
+      foreach ($site_domains as $site_domain) {
+        $host_pattern = '{^https?://' . \str_replace('.', '\.', $site_domain) . '/}';
+        if (\preg_match($host_pattern, $url)) {
+          $url = \preg_replace($host_pattern, '/', $url) ?: $url;
+          break;
+        }
+      }
+
+      $target_type = $target_id = NULL;
+
+      // Check if the href links to an entity.
+      $url_object = $this->pathValidator()->getUrlIfValidWithoutAccessCheck($url);
+
+      $entity_pattern = '/^entity\./';
+      $public_file_pattern = '{^/?' . $this->publicFileDirectory() . '/}';
+
+      if ($url_object && $url_object->isRouted() && \preg_match($entity_pattern, $url_object->getRouteName())) {
+        // Ge the target entity type and ID.
+        $route_parameters = $url_object->getRouteParameters();
+        $target_type = \array_keys($route_parameters)[0];
+        $target_id = $route_parameters[$target_type];
+      }
+      elseif (\preg_match($public_file_pattern, $url)) {
+        // Check if we can map the link to a public file.
+        $file_uri = \preg_replace($public_file_pattern, 'public://', \urldecode($url));
+        $files = $this->entityTypeManager->getStorage('file')->loadByProperties(['uri' => $file_uri]);
+        if ($files) {
+          // File entity found.
+          $target_type = 'file';
+          $target_id = \array_keys($files)[0];
+        }
+      }
+
+      $entity = NULL;
+      if ($target_type && $target_id) {
+        $entity = $this->entityTypeManager->getStorage($target_type)->load($target_id);
+      }
+
+      if (!($entity instanceof ContentEntityInterface)) {
+        // This module only supports content entity types.
+        $entity = NULL;
+      }
+
+      return $entity ?: NULL;
+    }
+    catch (\Exception $e) {
+      return NULL;
+    }
+  }
+
+  /**
+   * Returns the path validator service.
+   *
+   * @return \Drupal\Core\Path\PathValidatorInterface
+   *   The path validator.
+   */
+  protected function pathValidator() {
+    if (!$this->pathValidator) {
+      $this->pathValidator = $this->container()->get('path.validator');
+    }
+    return $this->pathValidator;
+  }
+
+  /**
+   * Return the public file directory path.
+   *
+   * @return string
+   *   The public file directory path.
+   */
+  protected function publicFileDirectory() {
+    if (!$this->publicFileDirectory) {
+      $this->publicFileDirectory = $this->container()->get('stream_wrapper.public')->getDirectoryPath();
+    }
+    return $this->publicFileDirectory;
+  }
+
+  /**
+   * Returns the service container.
+   *
+   * @return \Symfony\Component\DependencyInjection\ContainerInterface
+   *   The service container.
+   */
+  private function container() {
+    return \Drupal::getContainer();
+  }
+
 }
diff --git a/src/Plugin/EntityUsage/Track/EntityReference.php b/src/Plugin/EntityUsage/Track/EntityReference.php
index ea496d0..128b583 100644
--- a/src/Plugin/EntityUsage/Track/EntityReference.php
+++ b/src/Plugin/EntityUsage/Track/EntityReference.php
@@ -2,14 +2,9 @@
 
 namespace Drupal\entity_usage\Plugin\EntityUsage\Track;
 
-use Drupal\Core\Config\ConfigFactoryInterface;
 use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
 use Drupal\Core\Entity\ContentEntityInterface;
-use Drupal\Core\Entity\EntityFieldManagerInterface;
-use Drupal\Core\Entity\EntityTypeManagerInterface;
-use Drupal\entity_usage\EntityUsage;
 use Drupal\entity_usage\EntityUsageTrackBase;
-use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Tracks usage of entities related in entity_reference fields.
@@ -22,51 +17,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  */
 class EntityReference extends EntityUsageTrackBase {
 
-  /**
-   * The entity type manager.
-   *
-   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
-   */
-  protected $entityTypeManager;
-
-  /**
-   * Constructs display plugin.
-   *
-   * @param array $configuration
-   *   A configuration array containing information about the plugin instance.
-   * @param string $plugin_id
-   *   The plugin_id for the plugin instance.
-   * @param mixed $plugin_definition
-   *   The plugin implementation definition.
-   * @param \Drupal\entity_usage\EntityUsage $usage_service
-   *   The usage tracking service.
-   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
-   *   The EntityFieldManager service.
-   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
-   *   The config factory.
-   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
-   *   The EntityTypeManager service.
-   */
-  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityUsage $usage_service, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
-    parent::__construct($configuration, $plugin_id, $plugin_definition, $usage_service, $entity_field_manager, $config_factory);
-    $this->entityTypeManager = $entity_type_manager;
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
-    return new static(
-      $configuration,
-      $plugin_id,
-      $plugin_definition,
-      $container->get('entity_usage.usage'),
-      $container->get('entity_field.manager'),
-      $container->get('config.factory'),
-      $container->get('entity_type.manager')
-    );
-  }
-
   /**
    * {@inheritdoc}
    */
diff --git a/src/Plugin/EntityUsage/Track/HtmlLink.php b/src/Plugin/EntityUsage/Track/HtmlLink.php
index efea4dc..dc01295 100644
--- a/src/Plugin/EntityUsage/Track/HtmlLink.php
+++ b/src/Plugin/EntityUsage/Track/HtmlLink.php
@@ -3,15 +3,6 @@
 namespace Drupal\entity_usage\Plugin\EntityUsage\Track;
 
 use Drupal\Component\Utility\Html;
-use Drupal\Core\Config\ConfigFactoryInterface;
-use Drupal\Core\Entity\EntityFieldManagerInterface;
-use Drupal\Core\Entity\EntityRepositoryInterface;
-use Drupal\Core\Entity\EntityTypeManagerInterface;
-use Drupal\Core\Extension\ModuleHandlerInterface;
-use Drupal\Core\Path\PathValidatorInterface;
-use Drupal\Core\StreamWrapper\PublicStream;
-use Drupal\entity_usage\EntityUsage;
-use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Tracks usage of entities referenced from regular HTML Links.
@@ -24,79 +15,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  */
 class HtmlLink extends TextFieldEmbedBase {
 
-  /**
-   * Entity type manager service.
-   *
-   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
-   */
-  protected $entityTypeManager;
-
-  /**
-   * The Drupal Path Validator service.
-   *
-   * @var \Drupal\Core\Path\PathValidatorInterface
-   */
-  protected $pathValidator;
-
-  /**
-   * The public file directory.
-   *
-   * @var string
-   */
-  protected $publicFileDirectory;
-
-  /**
-   * Constructs the HtmlLink plugin.
-   *
-   * @param array $configuration
-   *   A configuration array containing information about the plugin instance.
-   * @param string $plugin_id
-   *   The plugin_id for the plugin instance.
-   * @param mixed $plugin_definition
-   *   The plugin implementation definition.
-   * @param \Drupal\entity_usage\EntityUsage $usage_service
-   *   The usage tracking service.
-   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
-   *   The EntityFieldManager service.
-   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
-   *   The factory for configuration objects.
-   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
-   *   The ModuleHandler service.
-   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
-   *   The EntityRepositoryInterface service.
-   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
-   *   The EntityTypeManager service.
-   * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
-   *   The Drupal Path Validator service.
-   * @param \Drupal\Core\StreamWrapper\PublicStream $public_stream
-   *   The Public Stream service.
-   */
-  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityUsage $usage_service, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, EntityRepositoryInterface $entity_repository, EntityTypeManagerInterface $entity_type_manager, PathValidatorInterface $path_validator, PublicStream $public_stream) {
-    parent::__construct($configuration, $plugin_id, $plugin_definition, $usage_service, $entity_field_manager, $config_factory, $module_handler, $entity_repository);
-    $this->entityTypeManager = $entity_type_manager;
-    $this->pathValidator = $path_validator;
-    $this->publicFileDirectory = $public_stream->getDirectoryPath();
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
-    return new static(
-      $configuration,
-      $plugin_id,
-      $plugin_definition,
-      $container->get('entity_usage.usage'),
-      $container->get('entity_field.manager'),
-      $container->get('config.factory'),
-      $container->get('module_handler'),
-      $container->get('entity.repository'),
-      $container->get('entity_type.manager'),
-      $container->get('path.validator'),
-      $container->get('stream_wrapper.public')
-    );
-  }
-
   /**
    * {@inheritdoc}
    */
@@ -112,43 +30,9 @@ class HtmlLink extends TextFieldEmbedBase {
       try {
         // Get the href value of the <a> element.
         $href = $element->getAttribute('href');
-
-        // Strip off the scheme and host, so we only get the path.
-        $site_domains = $this->config->get('site_domains') ?: [];
-        foreach ($site_domains as $site_domain) {
-          $host_pattern = '{^https?://' . str_replace('.', '\.', $site_domain) . '/}';
-          if (\preg_match($host_pattern, $href)) {
-            $href = preg_replace($host_pattern, '/', $href);
-            break;
-          }
-        }
-
-        $target_type = $target_id = NULL;
-
-        // Check if the href links to an entity.
-        $url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($href);
-        if ($url && $url->isRouted() && preg_match('/^entity\./', $url->getRouteName())) {
-          // Ge the target entity type and ID.
-          $route_parameters = $url->getRouteParameters();
-          $target_type = array_keys($route_parameters)[0];
-          $target_id = $route_parameters[$target_type];
-        }
-        elseif (\preg_match('{^/?' . $this->publicFileDirectory . '/}', $href)) {
-          // Check if we can map the link to a public file.
-          $file_uri = preg_replace('{^/?' . $this->publicFileDirectory . '/}', 'public://', urldecode($href));
-          $files = $this->entityTypeManager->getStorage('file')->loadByProperties(['uri' => $file_uri]);
-          if ($files) {
-            // File entity found.
-            $target_type = 'file';
-            $target_id = array_keys($files)[0];
-          }
-        }
-
-        if ($target_type && $target_id) {
-          $entity = $this->entityTypeManager->getStorage($target_type)->load($target_id);
-          if ($entity) {
-            $entities[$entity->uuid()] = $target_type;
-          }
+        $entity = $this->findEntityByUrl($href);
+        if ($entity) {
+          $entities[$entity->uuid()] = $entity->getEntityTypeId();
         }
       }
       catch (\Exception $e) {
diff --git a/src/Plugin/EntityUsage/Track/Link.php b/src/Plugin/EntityUsage/Track/Link.php
index 380c0cd..32ce1f3 100644
--- a/src/Plugin/EntityUsage/Track/Link.php
+++ b/src/Plugin/EntityUsage/Track/Link.php
@@ -2,15 +2,9 @@
 
 namespace Drupal\entity_usage\Plugin\EntityUsage\Track;
 
-use Drupal\Core\Config\ConfigFactoryInterface;
 use Drupal\Core\Entity\ContentEntityInterface;
-use Drupal\Core\Entity\ContentEntityTypeInterface;
-use Drupal\Core\Entity\EntityFieldManagerInterface;
-use Drupal\Core\Entity\EntityTypeManagerInterface;
-use Drupal\entity_usage\EntityUsage;
 use Drupal\entity_usage\EntityUsageTrackBase;
 use Drupal\link\LinkItemInterface;
-use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Tracks usage of entities related in entity_reference fields.
@@ -23,67 +17,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  */
 class Link extends EntityUsageTrackBase {
 
-  /**
-   * Entity field manager service.
-   *
-   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
-   */
-  protected $entityFieldManager;
-
-  /**
-   * Entity type manager service.
-   *
-   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
-   */
-  protected $entityTypeManager;
-
-  /**
-   * Constructs display plugin.
-   *
-   * @param array $configuration
-   *   A configuration array containing information about the plugin instance.
-   * @param string $plugin_id
-   *   The plugin_id for the plugin instance.
-   * @param mixed $plugin_definition
-   *   The plugin implementation definition.
-   * @param \Drupal\entity_usage\EntityUsage $usage_service
-   *   The usage tracking service.
-   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
-   *   The EntityFieldManager service.
-   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
-   *   The factory for configuration objects.
-   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
-   *   The EntityTypeManager service.
-   */
-  public function __construct(
-    array $configuration,
-    $plugin_id,
-    $plugin_definition,
-    EntityUsage $usage_service,
-    EntityFieldManagerInterface $entity_field_manager,
-    ConfigFactoryInterface $config_factory,
-    EntityTypeManagerInterface $entity_type_manager
-  ) {
-    parent::__construct($configuration, $plugin_id, $plugin_definition, $usage_service, $entity_field_manager, $config_factory);
-    $this->entityFieldManager = $entity_field_manager;
-    $this->entityTypeManager = $entity_type_manager;
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
-    return new static(
-      $configuration,
-      $plugin_id,
-      $plugin_definition,
-      $container->get('entity_usage.usage'),
-      $container->get('entity_field.manager'),
-      $container->get('config.factory'),
-      $container->get('entity_type.manager')
-    );
-  }
-
   /**
    * {@inheritdoc}
    */
@@ -183,21 +116,16 @@ class Link extends EntityUsageTrackBase {
    */
   protected function getTargetEntity(LinkItemInterface $link) {
     // Check if the link is referencing an entity.
-    $url = $link->getUrl();
-    if (!$url->isRouted() || !preg_match('/^entity\./', $url->getRouteName())) {
-      return NULL;
-    }
+    $url = $link->getString();
 
-    // Ge the target entity type and ID.
-    $route_parameters = $url->getRouteParameters();
-    $target_type = array_keys($route_parameters)[0];
-    $target_id = $route_parameters[$target_type];
-
-    if (!($this->entityTypeManager->getDefinition($target_type) instanceof ContentEntityTypeInterface)) {
-      // This module only supports content entity types.
+    $entity = $this->findEntityByUrl($url);
+    if (!$entity) {
       return NULL;
     }
 
+    $target_type = $entity->getEntityTypeId();
+    $target_id = $entity->id();
+
     // Glue the target type and ID together for easy comparison.
     return $target_type . '|' . $target_id;
   }
diff --git a/src/Plugin/EntityUsage/Track/TextFieldEmbedBase.php b/src/Plugin/EntityUsage/Track/TextFieldEmbedBase.php
index 54266f6..b4a21c9 100644
--- a/src/Plugin/EntityUsage/Track/TextFieldEmbedBase.php
+++ b/src/Plugin/EntityUsage/Track/TextFieldEmbedBase.php
@@ -5,6 +5,7 @@ namespace Drupal\entity_usage\Plugin\EntityUsage\Track;
 use Drupal\Core\Config\ConfigFactoryInterface;
 use Drupal\Core\Entity\EntityFieldManagerInterface;
 use Drupal\Core\Entity\EntityRepositoryInterface;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Extension\ModuleHandlerInterface;
 use Drupal\entity_usage\EmbedTrackInterface;
 use Drupal\entity_usage\EntityUsage;
@@ -50,9 +51,11 @@ abstract class TextFieldEmbedBase extends EntityUsageTrackBase implements EmbedT
    *   The ModuleHandler service.
    * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
    *   The EntityRepositoryInterface service.
+   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
+   *   The entity type manager servive.
    */
-  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityUsage $usage_service, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, EntityRepositoryInterface $entity_repository) {
-    parent::__construct($configuration, $plugin_id, $plugin_definition, $usage_service, $entity_field_manager, $config_factory);
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityUsage $usage_service, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, EntityRepositoryInterface $entity_repository, EntityTypeManagerInterface $entity_type_manager) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition, $usage_service, $entity_field_manager, $config_factory, $entity_type_manager);
     $this->moduleHandler = $module_handler;
     $this->entityRepository = $entity_repository;
   }
@@ -69,7 +72,8 @@ abstract class TextFieldEmbedBase extends EntityUsageTrackBase implements EmbedT
       $container->get('entity_field.manager'),
       $container->get('config.factory'),
       $container->get('module_handler'),
-      $container->get('entity.repository')
+      $container->get('entity.repository'),
+      $container->get('entity_type.manager')
     );
   }
 
