Skip to content

Commit

Permalink
feat: Интеграция с компонентом FetchIt
Browse files Browse the repository at this point in the history
  • Loading branch information
GulomovCreative committed Oct 2, 2023
1 parent 4494aa3 commit 3086a5f
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 65 deletions.
6 changes: 4 additions & 2 deletions assets/components/reachgoal/js/mgr/utils/utils.js
Expand Up @@ -36,7 +36,9 @@ Reachgoal.utils.renderActions = function (value, props, row) {
);
};


Reachgoal.utils.renderForm = function (value, props, row) {
return row.data.form_id || row.data.form_selector;
};

Reachgoal.utils.getMenu = function (actions, grid, selected) {
var menu = [];
Expand Down Expand Up @@ -92,4 +94,4 @@ Reachgoal.utils.getMenu = function (actions, grid, selected) {
}

return menu;
};
};
10 changes: 5 additions & 5 deletions assets/components/reachgoal/js/mgr/widgets/goals.grid.js
Expand Up @@ -44,15 +44,15 @@ Ext.extend(Reachgoal.grid.Goals, MODx.grid.Grid, {
return [
{dataIndex: 'id', sortable: true, width: 70, header: 'ID', hidden: true},
{dataIndex: 'event', sortable: true, width: 160, header: _('reachgoal_goals_grid_event')},
{dataIndex: 'form_id', sortable: true, width: 160, header: _('reachgoal_goals_grid_form_id')},
{dataIndex: 'form', sortable: true, width: 160, header: _('reachgoal_goals_grid_form_id'), renderer: Reachgoal.utils.renderForm},
{dataIndex: 'service', sortable: true, width: 150, header: _('reachgoal_goals_grid_service')},
{dataIndex: 'service_id', sortable: true, width: 140, header: _('reachgoal_goals_grid_service_id'), hidden: true},
{dataIndex: 'goal_name', sortable: true, width: 150, header: _('reachgoal_goals_grid_goal_name')},
{dataIndex: 'actions', width: 100, header: _('actions'), renderer: Reachgoal.utils.renderActions, sortable: false, id: 'actions'}
]
},
getFields: function () {
return ['id','event','form_id','service','service_id','goal_name','actions'];
return ['id','event','form_id','form_selector','service','service_id','goal_name','actions'];
},
createItem: function (btn, e) {
var id = 'reachgoal-goals-window-create';
Expand Down Expand Up @@ -204,7 +204,7 @@ Ext.extend(Reachgoal.grid.Goals, MODx.grid.Grid, {

return ids;
},

searchFields: ['query'],

filterSend: function () {
Expand All @@ -224,5 +224,5 @@ Ext.extend(Reachgoal.grid.Goals, MODx.grid.Grid, {
}
this.filterSend();
},
});
Ext.reg('reachgoal-grid-goals', Reachgoal.grid.Goals);
});
Ext.reg('reachgoal-grid-goals', Reachgoal.grid.Goals);
54 changes: 36 additions & 18 deletions assets/components/reachgoal/js/mgr/widgets/goals.windows.js
Expand Up @@ -3,7 +3,7 @@ Reachgoal.window.Goals = function (config) {
if (!config.id) {
config.id = 'reachgoal-goal-create';
}

Ext.applyIf(config, {
title: _('add'),
url: Reachgoal.config.connector_url,
Expand Down Expand Up @@ -31,7 +31,7 @@ Ext.extend(Reachgoal.window.Goals, MODx.Window, {
var fields = {
id: {xtype: 'hidden'},
event: {
xtype: 'reachgoal-combo-list',
xtype: 'reachgoal-combo-list',
action: 'mgr/goals/events',
allowBlank: false,
listeners: {
Expand All @@ -47,6 +47,11 @@ Ext.extend(Reachgoal.window.Goals, MODx.Window, {
allowBlank: true,
emptyText: _('reachgoal_goals_grid_empty_form_id'),
},
form_selector: {
xtype: 'textfield',
allowBlank: true,
emptyText: _('reachgoal_goals_grid_empty_form_selector'),
},
service: {
xtype: 'reachgoal-combo-list',
action: 'mgr/goals/services',
Expand Down Expand Up @@ -88,47 +93,60 @@ Ext.extend(Reachgoal.window.Goals, MODx.Window, {
return data;
},
checkEvent: function (config, firstload) {
var event = Ext.getCmp(config.id + '-event').getValue();

const event = Ext.getCmp(config.id + '-event').getValue();
const formIdField = Ext.getCmp(config.id + '-form_id');
const formSelectorField = Ext.getCmp(config.id + '-form_selector');

if (firstload) {
if (event != 'AjaxForm') {
this.hideField(Ext.getCmp(config.id + '-form_id'));
if (event !== 'AjaxForm') {
this.hideField(formIdField);
}

if (event !== 'FetchIt') {
this.hideField(formSelectorField);
}

return false;
}

if (event == 'AjaxForm') {
this.showField(Ext.getCmp(config.id + '-form_id'));
this.showField(formIdField);
} else {
this.hideField(Ext.getCmp(config.id + '-form_id'));
this.hideField(formIdField);
}

Ext.getCmp(config.id + '-form_id').reset();

if (event === 'FetchIt') {
this.showField(formSelectorField);
} else {
this.hideField(formSelectorField);
}

formIdField.reset();
formSelectorField.reset();
},
checkService: function (config, firstload) {
var service = Ext.getCmp(config.id + '-service').getValue();

if (firstload) {
if (service != 'metrika') {
this.hideField(Ext.getCmp(config.id + '-service_id'));
}

if (!service || service == 'metrika') {
this.hideField(Ext.getCmp(config.id + '-goal_category'));
}

return false;
}

if (service == 'metrika') {
this.showField(Ext.getCmp(config.id + '-service_id'));
this.hideField(Ext.getCmp(config.id + '-goal_category'));
} else {
this.hideField(Ext.getCmp(config.id + '-service_id'));
this.showField(Ext.getCmp(config.id + '-goal_category'));
}

Ext.getCmp(config.id + '-service_id').reset();
}
});
Expand All @@ -147,4 +165,4 @@ Reachgoal.window.UpdateGoals = function (config) {
Reachgoal.window.UpdateGoals.superclass.constructor.call(this, config);
};
Ext.extend(Reachgoal.window.UpdateGoals, Reachgoal.window.Goals, {});
Ext.reg('reachgoal-goals-window-update', Reachgoal.window.UpdateGoals);
Ext.reg('reachgoal-goals-window-update', Reachgoal.window.UpdateGoals);
48 changes: 36 additions & 12 deletions assets/components/reachgoal/js/web/default.js
Expand Up @@ -4,26 +4,27 @@ var Reachgoal = {
RemoveProduct: {},
Order: {},
Ajaxform: {},
FetchIt: [],
},
goal: function (service, service_id, goal_name, goal_category) {
switch (service) {
case 'metrika':
case 'metrika':
if (typeof ym != 'undefined') {
ym(service_id, 'reachGoal', goal_name);
} else {
window['yaCounter' + service_id].reachGoal(goal_name);
}

break;
case 'gtag':

case 'gtag':
if (goal_category) {
gtag('event', goal_name, {event_category: goal_category});
} else {
gtag('event', goal_name);
}
break;

case 'gtm':
if (goal_category) {
dataLayer.push({'event': 'event-to-ga', 'eventCategory' : goal_category, 'eventAction' : goal_name});
Expand Down Expand Up @@ -62,13 +63,36 @@ window.onload = function () {
}
}

$(document).on('af_complete', function(event, response) {
if (response.success && typeof Reachgoal.initialize.AjaxForm != 'undefined') {
if (
(window.$ || window.jQuery)
&& Reachgoal.initialize.Ajaxform
) {
$(document).on('af_complete', function(event, response) {
if (!response.success) {
return;
}

Reachgoal.initialize.AjaxForm.forEach(function(item) {
if (response.form.attr('id') == item.form_id) {
Reachgoal.goal(item.service, item.service_id, item.goal_name, item.goal_category);
if (response.form.attr('id') !== item.form_id) {
return;
}

Reachgoal.goal(item.service, item.service_id, item.goal_name, item.goal_category);
});
}
});
};
});
}
};

if (Reachgoal.initialize.FetchIt) {
document.addEventListener('fetchit:success', ({ detail }) => {
const { form } = detail;

Reachgoal.initialize.FetchIt.forEach((item) => {
if (!form.matches(item.form_selector)) {
return;
}

Reachgoal.goal(item.service, item.service_id, item.goal_name, item.goal_category);
})
})
}
5 changes: 3 additions & 2 deletions core/components/reachgoal/elements/plugins/ReachGoal.php
Expand Up @@ -19,7 +19,8 @@
'service_id' => $goal['service_id'] ?:$modx->getOption('reachgoal_yacounter_default'),
'goal_name' => $goal['goal_name'],
'goal_category' => $goal['goal_category'],
'form_id' => $goal['form_id']
'form_id' => $goal['form_id'],
'form_selector' => $goal['form_selector']
];
}

Expand All @@ -29,4 +30,4 @@
<script>
Reachgoal.initialize = '. json_encode($goals_list) .'
</script>');
}
}
6 changes: 5 additions & 1 deletion core/components/reachgoal/lexicon/en/default.inc.php
Expand Up @@ -15,15 +15,19 @@
$_lang['reachgoal_types_addproduct'] = 'Add to cart';
$_lang['reachgoal_types_removeproduct'] = 'Delete from recycle bin';
$_lang['reachgoal_types_order'] = 'Order';
$_lang['reachgoal_types_fetchit'] = 'Form submission (FetchIt)';
$_lang['reachgoal_types_ajaxform'] = 'Form submission (AjaxForm)';

$_lang['reachgoal_goals_grid_service'] = 'Service';
$_lang['reachgoal_goals_grid_form_id'] = 'Form';
$_lang['reachgoal_goals_grid_form_selector'] = 'CSS selector of forms';
$_lang['reachgoal_err_form_id_null'] = 'Write <b>id</b> of the form!';
$_lang['reachgoal_err_form_selector_null'] = 'Enter forms CSS selector';
$_lang['reachgoal_goals_grid_empty_form_id'] = 'Enter the form ID (html ID attribute)';
$_lang['reachgoal_goals_grid_empty_form_selector'] = 'Example: #my-form or .my-forms';
$_lang['reachgoal_goals_grid_service_id'] = 'The counter number';
$_lang['reachgoal_goals_grid_empty_service_id'] = 'Default will use the System setting reachgoal_yacounter_default';
$_lang['reachgoal_err_service_id_null'] = 'Fill in <b>Counter number</b>!';
$_lang['reachgoal_goals_grid_event'] = 'Event';
$_lang['reachgoal_goals_grid_goal_name'] = 'Goal / Action';
$_lang['reachgoal_goals_grid_goal_category'] = 'Category';
$_lang['reachgoal_goals_grid_goal_category'] = 'Category';
6 changes: 5 additions & 1 deletion core/components/reachgoal/lexicon/ru/default.inc.php
Expand Up @@ -15,15 +15,19 @@
$_lang['reachgoal_types_addproduct'] = 'Добавление в корзину';
$_lang['reachgoal_types_removeproduct'] = 'Удаление из корзины';
$_lang['reachgoal_types_order'] = 'Оформление заказа';
$_lang['reachgoal_types_fetchit'] = 'Отправка формы (FetchIt)';
$_lang['reachgoal_types_ajaxform'] = 'Отправка формы (AjaxForm)';

$_lang['reachgoal_goals_grid_service'] = 'Сервис';
$_lang['reachgoal_goals_grid_form_id'] = 'Форма';
$_lang['reachgoal_goals_grid_form_selector'] = 'CSS селектор форм';
$_lang['reachgoal_err_form_id_null'] = 'Напишите <b>id</b> формы!';
$_lang['reachgoal_err_form_selector_null'] = 'Введите селектор формы';
$_lang['reachgoal_goals_grid_empty_form_id'] = 'Введите идентификатор формы (html-атрибут id)';
$_lang['reachgoal_goals_grid_empty_form_selector'] = 'Например: #my-form или .my-forms';
$_lang['reachgoal_goals_grid_service_id'] = 'Номер счетчика';
$_lang['reachgoal_goals_grid_empty_service_id'] = 'По умолчанию будет использоваться Системная настройка reachgoal_yacounter_default';
$_lang['reachgoal_err_service_id_null'] = 'Заполните <b>Номер счетчика</b>!';
$_lang['reachgoal_goals_grid_event'] = 'Событие';
$_lang['reachgoal_goals_grid_goal_name'] = 'Цель / Действие';
$_lang['reachgoal_goals_grid_goal_category'] = 'Категория';
$_lang['reachgoal_goals_grid_goal_category'] = 'Категория';
13 changes: 7 additions & 6 deletions core/components/reachgoal/model/reachgoal.class.php
Expand Up @@ -29,33 +29,34 @@ function __construct(modX &$modx, array $config = [])
$this->modx->addPackage('reachgoal', $this->config['modelPath']);
$this->modx->lexicon->load('reachgoal:default');
}

private function services() {
return [
'metrika' => $this->modx->lexicon('reachgoal_service_metrika'),
'gtag' => $this->modx->lexicon('reachgoal_service_gtag'),
'gtm' => $this->modx->lexicon('reachgoal_service_gtm'),
];
}

private function types() {
return [
'AddProduct' => $this->modx->lexicon('reachgoal_types_addproduct'),
'RemoveProduct' => $this->modx->lexicon('reachgoal_types_removeproduct'),
'Order' => $this->modx->lexicon('reachgoal_types_order'),
'FetchIt' => $this->modx->lexicon('reachgoal_types_fetchit'),
'AjaxForm' => $this->modx->lexicon('reachgoal_types_ajaxform')
];
}

public function getServiceName($service) {
$list = $this->services();

return $list[$service];
}

public function getTypeName($type) {
$list = $this->types();

return $list[$type];
}
}
Expand Up @@ -12,6 +12,7 @@
array (
'service' => '',
'form_id' => '',
'form_selector' => '',
'service_id' => '',
'event' => '',
'goal_name' => '',
Expand All @@ -35,6 +36,14 @@
'null' => false,
'default' => '',
),
'form_selector' =>
array (
'dbtype' => 'varchar',
'precision' => '255',
'phptype' => 'string',
'null' => false,
'default' => '',
),
'service_id' =>
array (
'dbtype' => 'varchar',
Expand Down
Expand Up @@ -5,9 +5,10 @@
<object class="ReachgoalGoals" table="reachgoal_goals" extends="xPDOSimpleObject">
<field key="service" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
<field key="form_id" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
<field key="form_selector" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
<field key="service_id" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
<field key="event" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
<field key="goal_name" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
<field key="goal_category" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
</object>
</model>
</model>

0 comments on commit 3086a5f

Please sign in to comment.