You are here

function acquia_lift_update_targeting in Acquia Lift Connector 7

Takes whatever is in the 'lift_targeting' data property and converts it into the required campaign structure (including nested tests where needed).

Parameters

$agent: The agent to create the targeting structure for.

2 calls to acquia_lift_update_targeting()
AcquiaLiftWebTestTarget::testImplementTargetingStructure in tests/acquia_lift.test
acquia_lift_review_form_submit in ./acquia_lift.admin.inc
Submit callback for the campaign review form.

File

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

Code

function acquia_lift_update_targeting($agent) {
  if (empty($agent->data['lift_targeting'])) {
    return;
  }

  // First we need to figure out what existing tests we have running as nested
  // tests for this agent.
  $option_set = acquia_lift_get_option_set_for_targeting($agent->machine_name);
  $existing_structure = acquia_lift_get_structure_from_targeting($option_set);

  // Any target audience that has multiple variations in it has a test running.
  $existing_tests = $existing_options = array();
  foreach ($existing_structure as $audience => $variations) {
    if (count($variations) > 1) {
      $existing_tests[$audience] = $variations;
    }
    else {
      $existing_options[$audience] = reset($variations);
    }
  }

  // Now look at the tests and options required by the new structure.
  $rules = $new_tests = $new_options = array();
  foreach ($agent->data['lift_targeting'] as $new_audience => $variations) {
    if (count($variations) == 1) {
      $new_options[$new_audience] = reset($variations);
      continue;
    }

    // See if this variation combination already exists as a test.
    foreach ($existing_tests as $existing_audience => $vars) {
      if ($variations == $vars) {
        $rules[$new_audience] = $existing_audience;
      }
    }

    // If we didn't find an existing test corresponding to this combination,
    // we'll need to create a new one.
    if (!isset($rules[$new_audience])) {
      $new_tests[$new_audience] = $variations;
    }
  }
  foreach (array_keys($existing_tests) as $audience) {
    if (!in_array($audience, $rules)) {

      // Delete the old test if it's no longer being used.
      acquia_lift_delete_old_nested_test($agent, $audience);
    }
    else {

      // Change the audience for the test if necessary.
      $new_audience = array_search($audience, $rules);
      if ($new_audience != $audience) {
        acquia_lift_set_audience_for_test($agent, $audience, $new_audience);
      }
    }
  }
  foreach ($existing_options as $audience => $option) {
    if (!isset($agent->data['lift_targeting'][$audience])) {
      acquia_lift_remove_option_for_audience($agent, $audience);
    }
  }

  // Now create any new tests required by the new structure.
  foreach ($new_tests as $audience => $variations) {
    acquia_lift_add_new_test_for_audience($agent, $variations, $audience);
  }
  foreach ($new_options as $audience => $option) {
    acquia_lift_set_option_for_audience($agent, $option, $audience);
  }

  // Now clobber the "lift_targeting" data as we only use that to store *changes*
  // to the running targeting.
  $agent->data['lift_targeting'] = array();
  personalize_agent_save($agent);
}