getGoals();
if ($goalId)
{
$goal = DigitalPointBetterAnalytics_Model_Reporting::getGoalByGoalId($goalsAll, @$betterAnalyticsOptions['property_id'], @$betterAnalyticsOptions['api']['profile'], $goalId);
}
if ($_SERVER['REQUEST_METHOD'] != 'POST')
{
echo '
' . ($goalId ? esc_html__('Edit Goal', 'better-analytics') : esc_html__('Create Goal', 'better-analytics')) . '
';
}
else
{
check_admin_referer('create_edit-goal' . $nOnceSalt);
$errorMessage = array();
$goalObject = array(
'name' => sanitize_text_field(@$_POST['name']),
'value' => (@$_POST['value'] + 0),
'type' => sanitize_text_field(@$_POST['type']),
'active' => (@$_POST['active'] ? true : false),
'urlDestinationDetails' => array(),
'visitTimeOnSiteDetails' => array(),
'visitNumPagesDetails' => array(),
'eventDetails' => array(),
);
if (!$goalObject['name'])
{
$errorMessage[] = esc_html__('Goal name is required.', 'better-analytics');
}
// calling out slot on insert
if ($slot = absint(@$_POST['slot']))
{
$goalObject['id'] = $slot;
}
if (@$_POST['type'] == 'URL_DESTINATION')
{
$goalObject['urlDestinationDetails'] = array(
'url' => esc_url(@$_POST['destination_url']),
'caseSensitive' => (@$_POST['case_sensitive'] ? true : false),
'matchType' => @$_POST['destination_match_type'],
'firstStepRequired' => (@$_POST['first_step_required'] ? true : false),
);
if (!empty($_POST['funnel']) && is_array($_POST['funnel']))
{
$goalObject['urlDestinationDetails']['steps'] = array();
if (!empty($_POST['funnel']['url'][0]))
{
foreach ($_POST['funnel']['name'] as $step => $name)
{
$goalObject['urlDestinationDetails']['steps'][] = array(
'number' => ($step + 1),
'name' => sanitize_text_field($name),
'url' => esc_url($_POST['funnel']['url'][$step])
);
}
}
}
if (!$goalObject['urlDestinationDetails']['url'])
{
$errorMessage[] = esc_html__('Destination URL is required.', 'better-analytics');
}
}
elseif (@$_POST['type'] == 'VISIT_TIME_ON_SITE')
{
$goalObject['visitTimeOnSiteDetails'] = array(
'comparisonType' => 'GREATER_THAN',
'comparisonValue' => (absint(@$_POST['hours']) * 3600) + (absint(@$_POST['minutes']) * 60) + absint(@$_POST['seconds']),
);
}
elseif (@$_POST['type'] == 'VISIT_NUM_PAGES')
{
$goalObject['visitNumPagesDetails'] = array(
'comparisonType' => 'GREATER_THAN',
'comparisonValue' => absint(@$_POST['pages']),
);
}
elseif (@$_POST['type'] == 'EVENT')
{
$goalObject['eventDetails'] = array(
'useEventValue' => (@$_POST['use_event_value'] ? true : false),
'eventConditions' => array()
);
if (!empty($_POST['event_category_value']))
{
$goalObject['eventDetails']['eventConditions'][] = array(
'type' => 'CATEGORY',
'matchType' => $_POST['event_category'],
'expression' => $_POST['event_category_value']
);
}
if (!empty($_POST['event_action_value']))
{
$goalObject['eventDetails']['eventConditions'][] = array(
'type' => 'ACTION',
'matchType' => $_POST['event_action'],
'expression' => $_POST['event_action_value']
);
}
if (!empty($_POST['event_label_value']))
{
$goalObject['eventDetails']['eventConditions'][] = array(
'type' => 'LABEL',
'matchType' => $_POST['event_label'],
'expression' => $_POST['event_label_value']
);
}
if (!empty($_POST['event_value_value']))
{
$goalObject['eventDetails']['eventConditions'][] = array(
'type' => 'VALUE',
'comparisonType' => $_POST['event_value'],
'comparisonValue' => $_POST['event_value_value']
);
}
if (count($goalObject['eventDetails']['eventConditions']) == 0)
{
$errorMessage[] = esc_html__('You must specify a Category, Action or Label.', 'better-analytics');
}
}
$reportingClass = DigitalPointBetterAnalytics_Helper_Reporting::getInstance();
if (!$errorMessage && $profile = $reportingClass->getProfileByProfileId(@$betterAnalyticsOptions['api']['profile']))
{
if ($goalId)
{
$goal = $reportingClass->patchGoal($profile['accountId'], $profile['webPropertyId'], $profile['id'], $goalId, $goalObject);
}
else
{
$goal = $reportingClass->insertGoal($profile['accountId'], $profile['webPropertyId'], $profile['id'], $goalObject);
}
$reportingClass->deleteGoalCache();
$goals = $reportingClass->getGoals();
$goals = DigitalPointBetterAnalytics_Model_Reporting::filterGoalsByProfile($goals, @$betterAnalyticsOptions['property_id'], @$betterAnalyticsOptions['api']['profile'], $totals);
if ($goalId)
{
/* translators: %1$s = , %2$s = */
$noticeAtTop = '' . sprintf(esc_html__('Goal %1$supdated%2$s.'), '', '') . '
';
}
else
{
/* translators: %1$s = , %2$s = */
$noticeAtTop = '' . sprintf(esc_html__('Goal %1$screated%2$s.'), '', '') . '
';
}
}
}
}
if (empty($_REQUEST['action']) || $_REQUEST['action'] != 'create_edit' || $_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!empty($errorMessage))
{
echo '';
foreach($errorMessage as $error)
{
echo '
';
}
echo '
';
}
else
{
echo '';
}
}