From b3dadf81c5d8eb397d5f2be1498ce1ae33c51319 Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Wed, 20 Aug 2014 18:02:08 +0200 Subject: refs #5989 #6026 let users define a goal depending on an event and fix the examples and condition was not updated when user changes the match_attribute --- core/Tracker/GoalManager.php | 20 +- lang/en.json | 1 + plugins/Events/Actions/ActionEvent.php | 15 ++ plugins/Goals/API.php | 9 +- plugins/Goals/javascripts/goalsForm.js | 40 +++- plugins/Goals/templates/_addEditGoal.twig | 8 +- plugins/Goals/templates/_formAddGoal.twig | 9 +- plugins/Goals/templates/_listGoalEdit.twig | 5 +- plugins/Goals/tests/APITest.php | 228 +++++++++++++++++++++ .../PHPUnit/Fixtures/SomeVisitsAllConversions.php | 24 +++ ...ckGoalsAllowMultipleConversionsPerVisitTest.php | 4 +- ...itTime.getVisitInformationPerServerTime_day.xml | 25 ++- ...eConversionsPerVisit__VisitsSummary.get_day.xml | 16 +- 13 files changed, 374 insertions(+), 30 deletions(-) create mode 100644 plugins/Goals/tests/APITest.php diff --git a/core/Tracker/GoalManager.php b/core/Tracker/GoalManager.php index fbc73381aa..12e3865d2c 100644 --- a/core/Tracker/GoalManager.php +++ b/core/Tracker/GoalManager.php @@ -138,15 +138,29 @@ class GoalManager || ($attribute == 'file' && $actionType != Action::TYPE_DOWNLOAD) || ($attribute == 'external_website' && $actionType != Action::TYPE_OUTLINK) || ($attribute == 'manually') + || in_array($attribute, array('event_action', 'event_name', 'event_category')) && $actionType != Action::TYPE_EVENT ) { continue; } $url = $decodedActionUrl; - // Matching on Page Title - if ($attribute == 'title') { - $url = $action->getActionName(); + + switch ($attribute) { + case 'title': + // Matching on Page Title + $url = $action->getActionName(); + break; + case 'event_action': + $url = $action->getEventAction(); + break; + case 'event_name': + $url = $action->getEventName(); + break; + case 'event_category': + $url = $action->getEventCategory(); + break; } + $pattern_type = $goal['pattern_type']; $match = $this->isUrlMatchingGoal($goal, $pattern_type, $url); diff --git a/lang/en.json b/lang/en.json index abd9d25e92..b7dad2a8b2 100644 --- a/lang/en.json +++ b/lang/en.json @@ -930,6 +930,7 @@ "CaseSensitive": "Case sensitive match", "ChooseGoal": "Choose Goal", "ClickOutlink": "Click on a Link to an external website", + "SendEvent": "Send an event", "ColumnAverageOrderRevenueDocumentation": "Average Order Value (AOV) is the total revenue from all Ecommerce Orders divided by the number of orders.", "ColumnAveragePriceDocumentation": "The average revenue for this %s.", "ColumnAverageQuantityDocumentation": "The average quantity of this %s sold in Ecommerce orders.", diff --git a/plugins/Events/Actions/ActionEvent.php b/plugins/Events/Actions/ActionEvent.php index 3791de794d..9921c8d39b 100644 --- a/plugins/Events/Actions/ActionEvent.php +++ b/plugins/Events/Actions/ActionEvent.php @@ -38,6 +38,21 @@ class ActionEvent extends Action return (strlen($eventCategory) > 0 && strlen($eventAction) > 0); } + public function getEventAction() + { + return $this->request->getParam('e_a'); + } + + public function getEventCategory() + { + return $this->request->getParam('e_c'); + } + + public function getEventName() + { + return $this->request->getParam('e_n'); + } + public function getCustomFloatValue() { return $this->eventValue; diff --git a/plugins/Goals/API.php b/plugins/Goals/API.php index 43fff0bfdb..176cafad24 100644 --- a/plugins/Goals/API.php +++ b/plugins/Goals/API.php @@ -79,7 +79,7 @@ class API extends \Piwik\Plugin\API * * @param int $idSite * @param string $name - * @param string $matchAttribute 'url', 'title', 'file', 'external_website' or 'manually' + * @param string $matchAttribute 'url', 'title', 'file', 'external_website', 'manually', 'event_action', 'event_category' or 'event_name' * @param string $pattern eg. purchase-confirmation.htm * @param string $patternType 'regex', 'contains', 'exact' * @param bool $caseSensitive @@ -91,7 +91,7 @@ class API extends \Piwik\Plugin\API public function addGoal($idSite, $name, $matchAttribute, $pattern, $patternType, $caseSensitive = false, $revenue = false, $allowMultipleConversionsPerVisit = false) { Piwik::checkUserHasAdminAccess($idSite); - $this->checkPatternIsValid($patternType, $pattern); + $this->checkPatternIsValid($patternType, $pattern, $matchAttribute); $name = $this->checkName($name); $pattern = $this->checkPattern($pattern); @@ -141,7 +141,7 @@ class API extends \Piwik\Plugin\API Piwik::checkUserHasAdminAccess($idSite); $name = $this->checkName($name); $pattern = $this->checkPattern($pattern); - $this->checkPatternIsValid($patternType, $pattern); + $this->checkPatternIsValid($patternType, $pattern, $matchAttribute); Db::get()->update(Common::prefixTable('goal'), array( 'name' => $name, @@ -157,10 +157,11 @@ class API extends \Piwik\Plugin\API Cache::regenerateCacheWebsiteAttributes($idSite); } - private function checkPatternIsValid($patternType, $pattern) + private function checkPatternIsValid($patternType, $pattern, $matchAttribute) { if ($patternType == 'exact' && substr($pattern, 0, 4) != 'http' + && substr($matchAttribute, 0, 6) != 'event_' ) { throw new Exception(Piwik::translate('Goals_ExceptionInvalidMatchingString', array("http:// or https://", "http://www.yourwebsite.com/newsletter/subscribed.html"))); } diff --git a/plugins/Goals/javascripts/goalsForm.js b/plugins/Goals/javascripts/goalsForm.js index 643a9ce3af..400f3813c2 100644 --- a/plugins/Goals/javascripts/goalsForm.js +++ b/plugins/Goals/javascripts/goalsForm.js @@ -34,6 +34,25 @@ function showCancel() { }); } +function onMatchAttributeChange(matchAttribute) +{ + if ('event' === matchAttribute) { + $('.entityAddContainer .whereEvent').show(); + $('.entityAddContainer .whereUrl').hide(); + } else { + $('.entityAddContainer .whereEvent').hide(); + $('.entityAddContainer .whereUrl').show(); + } + + $('#match_attribute_name').html(mappingMatchTypeName[matchAttribute]); + $('#examples_pattern').html(mappingMatchTypeExamples[matchAttribute]); +} + +function updateMatchAttribute () { + var matchTypeId = $(this).val(); + onMatchAttributeChange(matchTypeId); +} + // init the goal form with existing goal value, if any function initGoalForm(goalMethodAPI, submitText, goalName, matchAttribute, pattern, patternType, caseSensitive, revenue, allowMultiple, goalId) { $('#goal_name').val(goalName); @@ -46,6 +65,14 @@ function initGoalForm(goalMethodAPI, submitText, goalName, matchAttribute, patte } else { $('select[name=trigger_type] option[value=visitors]').prop('selected', true); } + + if (0 === matchAttribute.indexOf('event')) { + $('select[name=event_type] option[value=' + matchAttribute + ']').prop('selected', true); + matchAttribute = 'event'; + } + + onMatchAttributeChange(matchAttribute); + $('input[name=match_attribute][value=' + matchAttribute + ']').prop('checked', true); $('input[name=allow_multiple][value=' + allowMultiple + ']').prop('checked', true); $('#match_attribute_name').html(mappingMatchTypeName[matchAttribute]); @@ -66,6 +93,7 @@ function initGoalForm(goalMethodAPI, submitText, goalName, matchAttribute, patte } function bindGoalForm() { + $('select[name=trigger_type]').click(function () { var triggerTypeId = $(this).val(); if (triggerTypeId == "manually") { @@ -79,10 +107,9 @@ function bindGoalForm() { } }); - $('input[name=match_attribute]').click(function () { - var matchTypeId = $(this).val(); - $('#match_attribute_name').html(mappingMatchTypeName[matchTypeId]); - $('#examples_pattern').html(mappingMatchTypeExamples[matchTypeId]); + $(document).bind('Goals.edit', function () { + $('input[name=match_attribute]').off('change', updateMatchAttribute); + $('input[name=match_attribute]').change(updateMatchAttribute); }); $('#goal_submit').click(function () { @@ -126,6 +153,11 @@ function ajaxAddGoal() { parameters.caseSensitive = 0; } else { parameters.matchAttribute = $('input[name=match_attribute]:checked').val(); + + if (parameters.matchAttribute === 'event') { + parameters.matchAttribute = $('select[name=event_type]').val(); + } + parameters.patternType = $('[name=pattern_type]').val(); parameters.pattern = encodeURIComponent($('input[name=pattern]').val()); parameters.caseSensitive = $('#case_sensitive').prop('checked') == true ? 1 : 0; diff --git a/plugins/Goals/templates/_addEditGoal.twig b/plugins/Goals/templates/_addEditGoal.twig index c8feb87bc5..f9449d8ce9 100644 --- a/plugins/Goals/templates/_addEditGoal.twig +++ b/plugins/Goals/templates/_addEditGoal.twig @@ -55,7 +55,8 @@ "url": "{{ 'Goals_URL'|translate }}", "title": "{{ 'Goals_PageTitle'|translate }}", "file": "{{ 'Goals_Filename'|translate }}", - "external_website": "{{ 'Goals_ExternalWebsiteUrl'|translate }}" + "external_website": "{{ 'Goals_ExternalWebsiteUrl'|translate }}", + "event": "{{ 'Events_Event'|translate }}" }; var mappingMatchTypeExamples = { "url": "{{ 'General_ForExampleShort'|translate }} {{ 'Goals_Contains'|translate("'checkout/confirmation'") }} \ @@ -67,7 +68,10 @@
{{ 'General_ForExampleShort'|translate }} {{ 'Goals_MatchesExpression'|translate("'(.*)\\\.zip'") }}", "external_website": "{{ 'General_ForExampleShort'|translate }} {{ 'Goals_Contains'|translate("'amazon.com'") }} \
{{ 'General_ForExampleShort'|translate }} {{ 'Goals_IsExactly'|translate("'http://mypartner.com/landing.html'") }} \ -
{{ 'General_ForExampleShort'|translate }} {{ 'Goals_MatchesExpression'|translate("'http://www.amazon.com\\\/(.*)\\\/yourAffiliateId'") }}" +
{{ 'General_ForExampleShort'|translate }} {{ 'Goals_MatchesExpression'|translate("'http://www.amazon.com\\\/(.*)\\\/yourAffiliateId'") }}", + "event": "{{ 'General_ForExampleShort'|translate }} {{ 'Goals_Contains'|translate("'video'") }} \ +
{{ 'General_ForExampleShort'|translate }} {{ 'Goals_IsExactly'|translate("'click'") }} \ +
{{ 'General_ForExampleShort'|translate }} {{ 'Goals_MatchesExpression'|translate("'(.*)_banner'") }}" }; bindGoalForm(); diff --git a/plugins/Goals/templates/_formAddGoal.twig b/plugins/Goals/templates/_formAddGoal.twig index 002df4954b..7bff75f8cb 100644 --- a/plugins/Goals/templates/_formAddGoal.twig +++ b/plugins/Goals/templates/_formAddGoal.twig @@ -25,6 +25,9 @@
+ + +

@@ -35,7 +38,11 @@ - {{ 'Goals_WhereThe'|translate }} + {{ 'Goals_WhereThe'|translate }}