首页 > 解决方案 > Drupal 8 Google Analytics 模块,以编程方式获取 ID

问题描述

我正在尝试获取在 /admin/config/system/google-analytics 中设置的 Google Analytics ID (UA-xxxxxxx-yy)。是否可以将它作为控制器或表单中的变量,也许使用服务?

标签: drupalgoogle-analyticsdrupal-8drupal-modules

解决方案


从定义的模式中google_analytics.schema.yml我们可以读到:

google_analytics.settings:
  type: config_object
  label: 'Google Analytics settings'
  mapping:
    account:
      type: string
      label: 'Web Property ID'
      ...

因此 Google Analytics ID 存储在 config 中,可以像这样检索:

$config = \Drupal::config('google_analytics.settings');
$id = $config->get('account');

推荐阅读