简体   繁体   English

Drupal 8 Google Analytics 模块,以编程方式获取 ID

[英]Drupal 8 Google Analytics module, get the ID programatically

I'm trying to get the Google Analytics ID (UA-xxxxxxx-yy), which is set in /admin/config/system/google-analytics.我正在尝试获取在 /admin/config/system/google-analytics 中设置的 Google Analytics ID (UA-xxxxxxx-yy)。 Is it possible to get it as a variable in a controller or a form, maybe using a service?是否可以将它作为 controller 或表单中的变量,也许使用服务?

From the schema defined in google_analytics.schema.yml we can read:google_analytics.schema.yml中定义的架构我们可以读到:

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

So the Google Analytics ID is stored in config and can be retrieved like this:因此 Google Analytics ID 存储在 config 中,可以这样检索:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM