You are here

function acquia_lift_report_audience in Acquia Lift Connector 7.2

Builds the audience-specific report.

Parameters

$form_state:

stdClass $personalization_data: Object reprsenting the personalization

stdClass $targeting_option_set: Object representing the targeting option set for hte personalization

string $audience: The audience to pull a report for

\AcquiaLiftAPI $lift_api: The lift API client wrapper.

$from: Start date for the report

$to: End date for the report

bool $use_old_stats: Whether to use the legacy reports (i.e. direct from the decision engine)

bool $retired: Whether this is a report for a retired test

Return value

mixed

2 calls to acquia_lift_report_audience()
AcquiaLiftWebTestReports::testAudienceReportLogic in tests/acquia_lift.test
acquia_lift_report in ./acquia_lift.admin.inc

File

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

Code

function acquia_lift_report_audience($form_state, $personalization_data, $targeting_option_set, $audience, AcquiaLiftAPI $lift_api, $from, $to, $use_old_stats = FALSE, $retired = FALSE) {
  $audience_type = isset($targeting_option_set->targeting[$audience]['osid']) ? 'test' : 'target';
  module_load_include('inc', 'acquia_lift', 'includes/AcquiaLiftLearnReport');
  $variations = acquia_lift_get_structure_from_targeting($targeting_option_set);
  if ($retired) {
    $audience_type = 'test';
    $tests = acquia_lift_get_retired_tests($personalization_data->machine_name);
    foreach ($tests as $test) {

      // If it's a retired test, then the $audience param is actually the machine
      // name of the test.
      if ($test->machine_name == $audience) {
        $audience = $test->data['lift_audience'];
        $test_os = personalize_option_set_load_by_agent($test->machine_name);
        $test_os = reset($test_os);
        $variations[$audience] = array_map(function ($option) {
          return $option['option_id'];
        }, $test_os->options);
        $retired_test = $test;

        // We'll need this if using the old reporting.
        break;
      }
    }
  }

  // Conversion report filters.
  $selected_goal = empty($form_state['values']['goal']) ? NULL : $form_state['values']['goal'];
  $selected_metric = empty($form_state['values']['metric']) ? 'rate' : $form_state['values']['metric'];
  $session_count = $goal_count = $confidence = 0;
  $winner = NULL;
  $daily_data = $experiment_results = array();
  $nested_agent = NULL;
  if ($audience_type == 'test') {
    if (isset($retired_test)) {
      $nested_agent = $retired_test;
    }
    else {
      $nested_os = personalize_option_set_load($targeting_option_set->targeting[$audience]['osid']);
      $nested_agent = personalize_agent_load($nested_os->agent);
    }
  }
  if ($audience_type == 'test' && $use_old_stats) {

    // Get our test results the old way, i.e. pre-reporting-API
    $agent_instance = personalize_agent_load_agent($nested_agent->machine_name);
    if ($agent_instance instanceof AcquiaLiftLearn) {
      $options = array(
        'start_time' => $from,
        'end_time' => $to,
        'goal' => $selected_goal,
      );
      $learn_report = AcquiaLiftReportFactory::create($agent_instance, $lift_api, $options);
      $session_count = $learn_report
        ->getSessionCount();
      $goal_count = $learn_report
        ->getGoalCount();
      $daily_data = $learn_report
        ->getDailyData();
      $experiment_results = $learn_report
        ->getAggregatedData();
    }
  }
  else {

    // Pull the stats directly from the reporting API.
    try {
      $audience_report = $lift_api
        ->getAudienceReport($personalization_data->machine_name, $audience, $variations[$audience], $audience_type, $from, $to);
      if (!empty($audience_report['overall_stats']) && !empty($audience_report['daily_stats'])) {
        foreach ($audience_report['overall_stats'] as $variation => $stats) {
          $session_count += $stats['decision_count'];
          $goal_count += $stats['conversion_count'];
        }
        $daily_data = _extract_daily_data($audience_report['daily_stats'], $personalization_data->machine_name);
        if (isset($audience_report['test_results'])) {
          $experiment_results = _extract_experiment_results($audience_report['test_results']);
          $confidence = !empty($audience_report['overall_confidence']) ? $audience_report['overall_confidence'] : 0;
          $winner = !empty($audience_report['winner']) ? $audience_report['winner'] : null;
        }
      }
      else {
        return array(
          '#markup' => t('No data available for this audience.'),
        );
      }
    } catch (AcquiaLiftException $e) {
      drupal_set_message(t('There was a problem retrieving the reporting data from the API'), 'error');
    }
  }

  // Audience Overview report section.
  $form['audience_overview_report'] = array(
    '#theme_wrappers' => array(
      'container',
    ),
    '#attributes' => array(
      'id' => 'acquia-lift-audience-overview-report',
      'class' => array(
        'acquia-lift-report-section',
        'clearfix',
      ),
    ),
  );
  $audience_overview_report = _build_overview_report($session_count, $goal_count);
  $form['audience_overview_report']['report'] = array(
    '#markup' => drupal_render($audience_overview_report),
    '#theme_wrappers' => array(
      'container',
    ),
    '#id' => 'acquia-lift-audience-overview-report-data',
  );
  $daily_report = _build_daily_report($daily_data, $personalization_data->machine_name, $audience);
  $form['daily_report'] = array(
    '#type' => 'container',
    'header' => array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'acquia-lift-report-section-header',
          'clearfix',
        ),
      ),
      'title' => array(
        '#type' => 'container',
        '#attributes' => array(
          'class' => array(
            'acquia-lift-report-section-title',
          ),
        ),
        'report_title' => array(
          '#markup' => '<h2>' . t('Daily conversion report') . '</h2>',
        ),
      ),
    ),
    '#attributes' => array(
      'id' => 'acquia-lift-daily-report',
      'class' => array(
        'lift-statistics',
      ),
    ),
  );
  $form['daily_report']['header']['options'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'acquia-lift-report-section-options',
      ),
    ),
    '#tree' => FALSE,
    'metric' => acquia_lift_report_experiment_metric_dropdown($selected_metric),
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Filter'),
    ),
  );

  // We only support per-goal reporting for the new reports.
  if (!$use_old_stats) {
    $form['daily_report']['header']['options']['goal'] = acquia_lift_report_goal_dropdown($personalization_data->machine_name, $selected_goal);
  }
  $form['daily_report']['report'] = array(
    '#markup' => drupal_render($daily_report),
    '#theme_wrappers' => array(
      'container',
    ),
    '#id' => 'acquia-lift-daily-report-data',
  );
  if ($audience_type == 'test') {
    $experiment_report = _build_experiment_report($experiment_results, $confidence, $winner, $personalization_data->machine_name, $audience, variable_get('acquia_lift_confidence_measure', 95));

    // Conversion details section.
    $form['experiment_report'] = array(
      '#type' => 'container',
      'header' => array(
        '#type' => 'container',
        '#attributes' => array(
          'class' => array(
            'acquia-lift-report-section-header',
            'clearfix',
          ),
        ),
        'title' => array(
          '#type' => 'container',
          '#attributes' => array(
            'class' => array(
              'acquia-lift-report-section-title',
            ),
          ),
          'report_title' => array(
            '#markup' => '<h2>' . t('Experiment') . '</h2>',
          ),
        ),
      ),
      'summary' => array(
        '#type' => 'container',
        '#attributes' => array(
          'class' => array(
            'acquia-lift-report-header-summary',
          ),
        ),
      ),
      '#attributes' => array(
        'id' => 'acquia-lift-experiment-report',
        'class' => array(
          'acquia-lift-report-section',
        ),
      ),
    );

    // Show the test (explore)  percentage if in adaptive mode.
    if ($nested_agent->data['decision_style'] == 'adaptive') {
      $percentage = $nested_agent->data['explore_rate'];
      $form['experiment_report']['header']['title']['groups'] = array(
        '#markup' => t('(@percentage%)', array(
          '@percentage' => $percentage,
        )),
      );

      // Also add help text to the heading to explain what this is.
      $form['experiment_report']['header']['title']['#attributes']['data-help-tooltip'] = t('The portion of visitors who were shown a random variation, as opposed to the optimized variation. The data below are for the experiment group only.');
    }
    $form['experiment_report']['header']['summary']['report_summary'] = array(
      '#theme_wrappers' => array(
        'container',
      ),
      '#markup' => t('See which content variations are winning.'),
      '#attributes' => array(
        'class' => array(
          'acquia-lift-report-summary',
        ),
      ),
    );
    $form['experiment_report']['report'] = array(
      '#markup' => drupal_render($experiment_report),
      '#theme_wrappers' => array(
        'container',
      ),
      '#id' => 'acquia-lift-experiment-report-data',
    );
  }
  return $form;
}