It would be nice if the site-name was an option for the Merge Variables, in case multiple websites are subscribing people to the same list and you want to segment them. I suppose you could solve this problem by making a separate Mailing List, but there are probably other use cases.

I made it happen by just adding this code to the end of mailchimp_mailchimp_merge_keys() to include the Global tokens:

<?php if (function_exists('token_get_list')) {
    $tokens = token_get_list(array('global', 'order'));
    if (is_array($tokens['global'])) {
      foreach ($tokens['global'] as $token => $name) {
        $out['token_'. $token] = t('Token: !field', array('!field' => $name));
      }
    }
  } ?>

Another option that might have worked for me would be a "custom key" option, so I could enter the site name by hand, but the Global Tokens was a faster solution for me.

Thanks for the great module! Works like a charm and it was just what we needed.

Comments

Brian294’s picture

I second support for global tokens (actually ALL tokens). I am the author of the Google Analytics Tokenizer module and I need to get my tasty little tokens into mail chimp. http://drupal.org/project/ga_tokenizer

The code you have proposed certainly adds the token names to the list, but there are still some problems. 1. User tokens no longer display on the list. 2. Global tokens don't get merged -- at least for me.

It appears a second function is doing the actual merging into mail chimp and "user" is hard-coded into the routine:

/**
 * Get the user tokens for merging
 */
function _mailchimp_get_user_tokens($user) {
  $out = array();
  if (function_exists('token_get_values')) {
    $vars = token_get_values('user', $user);
    foreach ($vars->tokens as $key => $value) {
      $out['token_'. $value] = $vars->values[$key];
    }
  }
  return $out;
}
levelos’s picture

Status: Active » Closed (works as designed)

The module actually exposed 2 hooks, hook_mailchimp_merge_keys() and hook_mailchimp_merge_values() so any other module can add other merge values as needed.

Brian294’s picture

Status: Closed (works as designed) » Needs review

I actually tested those hooks (hook_mailchimp_merge_keys and hook_mailchimp_merge_values) and they didn't work.

Brian294’s picture

I should probably give you some sample code to justify my fussy-ness.

/**
 * Implementation of hook_mailchimp_merge_keys()
 */
function ga_tokenizer_mailchimp_merge_keys() {
  return array(
    'ga-source' => 'ga-source: Search engine, newsletter name, or other source.',
    'ga-campaign' => 'ga-campaign: To identify a specific product promotion or strategic campaign.',
    'ga-medium' => 'ga-medium: A medium such as email or cost-per-click.',
    'ga-content' => 'ga-content: Used for A/B testing. To differentiate ads or links that point to the same URL.',
    'ga-term' => 'ga-term: The keywords for the organic search or ad.',
    'ga-first-visit' => 'ga-first-visit: Date/Time of initial visit.',
    'ga-previous-visit' => 'ga-previous-visit: Date/Time of previous visit.',
    'ga-current-visit' => 'ga-current-visit: Date/Time of current visit.',
    'ga-times-visited' => 'ga-times-visited: Total number of times visited.'
  );
}

/**
 * Implementation of hook_mailchimp_merge_values()
 */
function ga_tokenizer_mailchimp_merge_values($user) {
    $out = array(
    'ga-source' => token_replace("[ga-source]"),
    'ga-campaign' => token_replace("[ga-campaign]"),
    'ga-medium' => token_replace("[ga-medium]"),
    'ga-content' => token_replace("[ga-content]"),
    'ga-term' => token_replace("[ga-term]"),
    'ga-first-visit' => token_replace("[ga-first-visit]"),
    'ga-previous-visit' => token_replace("[ga-previous-visit]"),
    'ga-current-visit' => token_replace("[ga-current-visit]"),
    'ga-times-visited' => token_replace("[ga-times-visited]")
  );
 return $out;
}
levelos’s picture

Status: Needs review » Closed (cannot reproduce)

Not sure w/o debugging your code. The implementation is very straightforward:


/**
 * Get the available merge var keys
 */
function mailchimp_get_merge_keys() {
  return module_invoke_all('mailchimp_merge_keys');
}

/**
 * Get all the potential merge var values for a given user
 */
function mailchimp_get_merge_values($uid) {
  if ($user = user_load(array('uid' => $uid))) {
    return module_invoke_all('mailchimp_merge_values', $user);
  }
  return array();
}
Brian294’s picture

Status: Closed (cannot reproduce) » Needs review

mailchimp_get_merge_keys will return my list of variables.

mailchimp_get_merge_values is where the problem is. It probably needs to look more like this to accomodate for non-authenticated users.

function mailchimp_get_merge_values($uid) {
  if ($user = user_load(array('uid' => $uid))) {
    return module_invoke_all('mailchimp_merge_values', $user);
  } else {
    return module_invoke_all('mailchimp_merge_values');
  }
  return array();
}
levelos’s picture

Status: Needs review » Closed (works as designed)

Brian - The module is actually not current designed to process merge values for anonymous users at all. The call to mailchimp_get_merge_values() is not made when an anonymous form is submitted. All merge values are taken from the form directly. If an "anonymous form" is presented to authenticated users, however, merge values are grabbed as defaults for the form fields.

So I'm not sure the point in allowing the call to mailchimp_get_merge_values() for anonymous users. You could always take the approach of using form alter to add hidden fields to the subscription form, which will then get posted as merge values to MC when it's submitted.