In our YII app, we would like to capture google analytics on validation failures. We are using modal rules for validating our data. Here is one of our validation modal code snippet
public function rules() {
return [
['email', 'validateEmail'],
];
}
public function validateEmail($attribute_name, $params) {
if (EMAIL EXITS IN DATABASE) {
$this->addError($attribute_name, Yii::t('app', 'Email Already Exist.'));
return false;
}
}
It correctly shows an error message on our frontend but we can not track this in google analytics. We would like to call a javascript function whenever this validation fails. Here is our javascript function
function captureGAEvent(category, action, label) {
alert(category + " " + action + " " + label);
ga('send', 'event', category, action, label);
}
Please guide.
yii2 documentation states that you could set up Client Side Validation
Which one has a clientValidateAttribute method Implementing Client Side Validation where you could put your analytics code as well.
You will need to enable enableClientValidation in your ActiveForm.
I would do following based on Deferred Validation paragraph.
Create a Validator Class
Use it in your model