edisonlabs / adobe_analytics
将Adobe Analytics JavaScript跟踪代码添加到您的网站页面。
1.8.0
2024-04-11 21:32 UTC
Requires
- drupal/token: 1.13
README
将Adobe Analytics系统添加到您的网站。
要求
- Token模块。
安装
像安装其他模块一样通过Drush或网络管理界面安装。
配置
要启用Adobe Analytics模块的环境模式,请在settings.php文件中添加以下配置:
$config['adobe_analytics'] = [ 'mode' => 'dev/prod', ];
设置为dev将执行开发脚本,设置为prod将执行生产脚本。
要添加脚本,请导航到管理 > 配置 > Adobe Analytics,其中包含主要配置设置。这些设置适用于所有非管理页面,并支持基于请求用户角色的附加条件。
如果您需要覆盖或扩展特定实体的设置,则向实体包添加一个Adobe Analytics类型的字段。完成此操作后,实体的编辑表单将显示Adobe Analytics部分,您可以在其中覆盖全局设置。
API
以下是一个示例模块代码,您可以使用它通过利用hook_adobe_analytics_variables()创建更适合跟踪需求的变量。此代码应放在您的自定义模块的.module文件中,并根据需要进行修改。为了说明目的,我们向adobe_anlaytics管理表单添加了一个设置,允许站点管理员控制是否跟踪我们的自定义“referring_search_engine”变量。
注意:不要忘记重命名函数。
/** * @file */ /** * Implements hook_adobe_analytics_variables(). */ function mymodule_adobe_analytics_variables() { // Initialize a variables array to be returned by this hook. $variables = []; $config_var = \Drupal::config('adobe_anlaytics.settings') ->get('track_search_engine', 0); if ($config_var) { $variables['referring_search_engine'] = 'none'; // Create a list of possible search engines that my site cares about. $search_engines = ['google.com', 'yahoo.com', 'bing.com', 'ask.com']; // Get the referring URL. $referer = $_SERVER['HTTP_REFERER']; // Check the URL to see if the request is coming from a search engine // and if it is, change the value of my "referring_search_engine" variable. foreach ($search_strings as $engine) { if (stripos($referer, $engine) !== FALSE) { $variables['referring_search_engine'] = $engine; break; } } } // Lets assume we need a variable called "date" and that for some reason it // *must* come before all the other variables. Note that if you have a // variable that must come at the end you can use "footer" as well. $header = ['date' => date('Ymd')]; return ['variables' => $variables, 'header' => $header]; } /** * Implements hook_form_FORM_ID_alter(). */ function mymodule_form_adobe_anlaytics_admin_settings_alter(&$form, &$form_state) { $form['general']['adobe_anlaytics_track_search_engine'] = [ '#type' => 'checkbox', '#title' => t('Track the referring search engine for every request'), '#default_value' => \Drupal::config(adobe_anlaytics . settings) ->get('track_search_engine', 0), ]; }