googleanalytics.variable.inc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * @file
  4. * Definition of variables for Variable API module.
  5. */
  6. /**
  7. * Implements hook_variable_info().
  8. */
  9. function googleanalytics_variable_info($options) {
  10. $variables['googleanalytics_account'] = array(
  11. 'type' => 'string',
  12. 'title' => t('Web Property ID', array(), $options),
  13. 'default' => 'UA-',
  14. 'description' => t('This ID is unique to each site you want to track separately, and is in the form of UA-xxxxxxx-yy. To get a Web Property ID, <a href="@analytics">register your site with Google Analytics</a>, or if you already have registered your site, go to your Google Analytics Settings page to see the ID next to every site profile. <a href="@webpropertyid">Find more information in the documentation</a>.', array('@analytics' => 'http://www.google.com/analytics/', '@webpropertyid' => url('https://developers.google.com/analytics/resources/concepts/gaConceptsAccounts', array('fragment' => 'webProperty'))), $options),
  15. 'required' => TRUE,
  16. 'group' => 'googleanalytics',
  17. 'localize' => TRUE,
  18. 'validate callback' => 'googleanalytics_validate_googleanalytics_account',
  19. );
  20. return $variables;
  21. }
  22. /**
  23. * Implements hook_variable_group_info().
  24. */
  25. function googleanalytics_variable_group_info() {
  26. $groups['googleanalytics'] = array(
  27. 'title' => t('Google Analytics'),
  28. 'description' => t('Configure tracking behavior to get insights into your website traffic and marketing effectiveness.'),
  29. 'access' => 'administer google analytics',
  30. 'path' => array('admin/config/system/googleanalytics'),
  31. );
  32. return $groups;
  33. }
  34. /**
  35. * Validate Web Property ID variable.
  36. */
  37. function googleanalytics_validate_googleanalytics_account($variable) {
  38. // Replace all type of dashes (n-dash, m-dash, minus) with the normal dashes.
  39. $variable['value'] = str_replace(array('–', '—', '−'), '-', $variable['value']);
  40. if (!preg_match('/^UA-\d{4,}-\d+$/', $variable['value'])) {
  41. return t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.');
  42. }
  43. }