Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ymeiner’s picture

Issue summary: View changes
ymeiner’s picture

ymeiner’s picture

Title: allow tokens in before and after js fields » Allow tokens in before and after js fields
hass’s picture

Status: Active » Needs work
Issue tags: -google analytics, -custom code snippet, -token replacement

Thanks for sharing. Please add some tests with examples. And do not forget to add the input validation we used in custom variables.

hass’s picture

  1. +++ b/googleanalytics.module
    @@ -183,7 +183,16 @@ function googleanalytics_page_alter(&$page) {
    +    $codesnippet_before = token_replace($codesnippet_before, $types, array('clear' => TRUE));
    +    $codesnippet_after = token_replace($codesnippet_after, $types, array('clear' => TRUE));
    

    These also need to call drupal_json_encode callback for every value or code injection is possible and JS code can break.

  2. +++ b/googleanalytics.module
    @@ -183,7 +183,16 @@ function googleanalytics_page_alter(&$page) {
    +    ¶
    

    TAB

KeyboardCowboy’s picture

Status: Needs work » Needs review
FileSize
4.71 KB

This patch adds the token tree to the admin page, validates against PII tokens (like v1.x variables did) and runs the tokens through drupal_json_encode.

hass’s picture

Version: 7.x-2.x-dev » 8.x-2.x-dev

Thanks a lot for the patch. Can you port this to D8 too, please?

KeyboardCowboy’s picture

I would love to port this to D8 as well, however I am pretty far behind in my D8 training and my time is very scarce right now. If I can find the time to clean up the issues on my own modules I will make this a higher priority.

hass’s picture

Version: 8.x-2.x-dev » 7.x-2.x-dev
FileSize
3.82 KB

I'm not really sure about the use cases here if I look into the token list. Can you share some examples that make it worth adding the performance penalty, please?

There is also a known problem I have not tested myself if taxonomy/term pages are overridden with a view. I have optimized and re-roled the patch a bit. "user" seems not required as token_tree shows current-user by default.

The rest of the code looks really good, but we need tests for this. May also add a test with views as it's documented to be an issue.

KeyboardCowboy’s picture

Thanks, @hass.

Our use case is that we wanted to track progress through multi-page webforms and we were using 'events' or 'custom dimensions' to get the data to GA since the URL for a multipage webform does not change. The only way I could find to do this was to create a custom token to represent the page of the webform the user was on and pass that into the GA Before field. It's working great for us.

I based the solution on the way custom variables worked in the older GA version.

hass’s picture

Are you able to share these code examples so we may use it later in a tests?

KeyboardCowboy’s picture

The following code generates the tokens.

/**
 * Implements hook_tokens()
 */
function mymod_tokens($type, $tokens, $data) {
  $replacements = array();
  if ($type == 'node') {
    $node = $data['node'];

    // Additional node token for the current webform page.
    // Token value is generated in the form_alter
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'webform-page':
          $replacements[$original] = $node->webform['tokens']['current-page'];
          break;
      }
    }
  }

  return $replacements;
}
/**
 * Implements hook_form_alter().
 */
function mymod_form_alter(&$form, &$form_state, $form_id) {
  // Webforms
  if (isset($form['#node']->webform)) {
    $webform = &$form['#node']->webform;
    $details = $form['details'];

    // Build a variable to represent the current webform page.
    $wf_name = ($webform['machine_name'] ? $webform['machine_name'] : drupal_html_class($form['#node']->title));
    $current = $details['page_num']['#value'];
    $total   = $details['page_count']['#value'];
    $var     = "{$wf_name}-{$current}-{$total}";

    $webform['tokens']['current-page'] = $var;
  }
}

Then I add this into the GA "Code snippet (before)" UI:

ga('send', 'event', 'webform', 'progress', [node:webform-page]);

hass’s picture

Version: 7.x-2.x-dev » 8.x-2.x-dev
Status: Needs review » Needs work
Plazik’s picture

ga('send', 'event', 'webform', 'progress', [node:webform-page]);

This event will be sent to GA on every page view.

It's better to send this event only on webform pages using custom hook which we are developing at #231451: Add hook to alter data before sending it to browser.

harsha012’s picture

Status: Needs review » Needs work

The last submitted patch, 15: allow-tokens-in-and-before-js-2207089-15.patch, failed testing.

The last submitted patch, 15: allow-tokens-in-and-before-js-2207089-15.patch, failed testing.

The last submitted patch, 15: allow-tokens-in-and-before-js-2207089-15.patch, failed testing.

The last submitted patch, 15: allow-tokens-in-and-before-js-2207089-15.patch, failed testing.

The last submitted patch, 15: allow-tokens-in-and-before-js-2207089-15.patch, failed testing.

hass’s picture

Has someone tested if the clear => TRUE may destroy JS code?

e.g. [name*='man'] may look like a token.

$( "input[name*='man']" ).val( "has man in it!" );
hass’s picture

Issue tags: +Needs tests
hass’s picture

Do you plan to update the patch with tests or should we close the case because of inactivity?

vakulrai’s picture

Status: Needs review » Needs work
FileSize
172.62 KB
172.62 KB

i have applied the patch give by #21 but it is breaking when applied.
My working drupal branch is 8.6.0

japerry’s picture

Status: Needs work » Closed (outdated)

With the sunset of legacy google analytics, the 8.x-2.x module is now unsupported. If this is still an issue with the 4.x version, please file a new issue in the queue.