You are here

function acquia_lift_target_audience_save in Acquia Lift Connector 7.2

Same name and namespace in other branches
  1. 7 acquia_lift.admin.inc \acquia_lift_target_audience_save()

Saves a target audience for an agent.

Parameters

$label: The human-readable name of the audience to save.

$agent_name: THe name of the agent to add the audience to.

$contexts: The contexts that make up the audience definition.

$strategy: The strategy to use for multiple contexts, i.e. 'AND' or 'OR'

number $weight: The weight for this audience within all audiences for the agent. Audiences with the lowest weight will be evaluated first.

$machine_name: The machine name for the audience if it already exists.

7 calls to acquia_lift_target_audience_save()
AcquiaLiftWebTestBase::createTargetAudience in tests/acquia_lift.test
Helper that adds a target audience using two contexts AND'd together.
AcquiaLiftWebTestReports::testReportPage in tests/acquia_lift.test
AcquiaLiftWebTestTarget::testAudienceName in tests/acquia_lift.test
AcquiaLiftWebTestTarget::testCreateTargetAudiences in tests/acquia_lift.test
acquia_lift_personalize_campaign_wizard_targeting_submit in ./acquia_lift.admin.wizard.inc
Submit function for targeting form.

... See full list

File

./acquia_lift.admin.inc, line 1527
acquia_lift.admin.inc Provides functions needed for the admin UI.

Code

function acquia_lift_target_audience_save($label, $agent_name, $contexts, $strategy, $weight = 50, $machine_name = NULL) {
  module_load_include('inc', 'personalize', 'personalize.admin');

  // Find the option set to use for targeting and add the audience.
  $option_set = acquia_lift_get_option_set_for_targeting($agent_name);
  if (empty($option_set)) {
    return FALSE;
  }
  if (empty($machine_name)) {
    if (is_numeric($label)) {
      $label = "Audience " . $label;
    }
    $machine_name = personalize_generate_machine_name($label, NULL, '-');
  }
  elseif (is_numeric($machine_name) && !isset($option_set->targeting[$machine_name])) {
    $machine_name = personalize_generate_machine_name("audience-" . $machine_name, NULL, '-');
  }
  if (!isset($option_set->targeting[$machine_name])) {
    $option_set->targeting[$machine_name] = array();
  }
  $option_set->targeting[$machine_name]['label'] = $label;
  $option_set->targeting[$machine_name]['weight'] = $weight;

  // Generate the feature strings and rules for the contexts.
  $agent = personalize_agent_load_agent($agent_name);
  $feature_strings = $feature_rules = array();
  foreach ($contexts as $context_values) {
    list($plugin_name, $context_name) = explode(PERSONALIZE_TARGETING_ADMIN_SEPARATOR, $context_values['context']);

    // Generate a value code based on the operator used.
    $value = personalize_targeting_generate_value_code($context_values['match'], $context_values['operator']);

    // Create a feature string for this context value that can be consumed
    // by the agent that will be using it.
    $feature_string = $agent
      ->convertContextToFeatureString($context_name, $value);
    $feature_strings[] = $feature_string;

    // Save the actual rule information as this is what will be used
    // for evaluating it.
    $feature_rules[$feature_string] = $context_values;

    // Override the context to split it into plugin and context parts.
    $feature_rules[$feature_string]['context'] = $context_name;
    $feature_rules[$feature_string]['plugin'] = $plugin_name;
  }
  $option_set->targeting[$machine_name]['targeting_features'] = $feature_strings;
  $option_set->targeting[$machine_name]['targeting_rules'] = $feature_rules;
  $option_set->targeting[$machine_name]['targeting_strategy'] = $strategy;
  try {
    personalize_option_set_save($option_set);
    return TRUE;
  } catch (PersonalizeException $e) {
    return FALSE;
  }
}