Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for CD and CV to Matomo Tag and Variables #201

Open
wants to merge 1 commit into
base: 4.x-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Context/BaseContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ private function parametersToVariableJs($container, $entity)
&& Variable::hasFieldConfigVariableParameter($parameter['uiControlAttributes']['field2'])) {
$parameterTemplateTypes[] = $parameter['name'] . $keyTemplateTypeSeparator . $parameter['uiControlAttributes']['field2']['key'];
}

if (!empty($parameter['uiControlAttributes']['field3']['key'])
&& Variable::hasFieldConfigVariableParameter($parameter['uiControlAttributes']['field3'])) {
$parameterTemplateTypes[] = $parameter['name'] . $keyTemplateTypeSeparator . $parameter['uiControlAttributes']['field3']['key'];
}

if (!empty($parameter['uiControlAttributes']['field4']['key'])
&& Variable::hasFieldConfigVariableParameter($parameter['uiControlAttributes']['field4'])) {
$parameterTemplateTypes[] = $parameter['name'] . $keyTemplateTypeSeparator . $parameter['uiControlAttributes']['field4']['key'];
}
}

}
Expand Down
6 changes: 6 additions & 0 deletions Model/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ public static function hasFieldConfigVariableParameter($parameter)
if (!empty($parameter['uiControlAttributes']['field2']) && self::hasFieldConfigVariableParameter($parameter['uiControlAttributes']['field2'])) {
return true;
}
if (!empty($parameter['uiControlAttributes']['field3']) && self::hasFieldConfigVariableParameter($parameter['uiControlAttributes']['field3'])) {
return true;
}
if (!empty($parameter['uiControlAttributes']['field4']) && self::hasFieldConfigVariableParameter($parameter['uiControlAttributes']['field4'])) {
return true;
}
}
if (!empty($parameter['uiControlAttributes']['parseVariables'])) {
// workaround for some variables that don't use above templates but still need to be parsed
Expand Down
79 changes: 78 additions & 1 deletion Template/Tag/MatomoTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,84 @@ public function getParameters()
}
return $value;
};
})
}),
$this->makeSetting('customDimensions', array(), FieldConfig::TYPE_ARRAY, function (FieldConfig $field) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaelHeerklotz Not 100% sure I understand why they are also needed in the Tag and not just in the variable?

Copy link
Author

@MichaelRoosz MichaelRoosz Nov 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsteur As I understand it, triggering Custom Variables or Dimensions in a Tag gives more flexibility, and allows having multiple tags with different Custom Vars/Dims while using the same Matomo Config Variable. I am not a power user yet, but my colleagues requested this feature.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. In case you can find out just also for our understanding: Why is it better to have multiple tags over multiple configurations? Of course when you have multiple variables then it means you still need multiple tags so it be maybe a bit more work using the variables. Just wanting to avoid suddenly having heaps of configurations in the tag that are also in the variable (and more and more coming in their over time) and confusing users because there are two ways to do things. I can kind of see it though for custom dimensions and custom variables as they might be in PAGE/ACTION scope and the variable should maybe be used mostly for VISIT scope.

Wonder if we should explain this in the description that these are maybe mostly meant for custom dimensions in scope action/page?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so, too. I think it makes sense having them both in the tag and the variable because it makes sense if they are of scope page.

Some explantion would help for sure.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add some explanations? Is there anything else missing for a merge?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing else missing to merge but the explanation. This will help users to know when to use what eg MatomoConfig custom dimensions vs Matomo tag custom dimensions.

We'll need to fix the tests then but can do this after merging.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaelRoosz any chance you could add the explanation as discussed so we can merge?

$field->title = 'Custom Dimensions';
$field->description = 'Optionally set one or multiple custom dimensions.';
$field->validate = function ($value) {
if (empty($value)) {
return;
}
if (!is_array($value)) {
throw new \Exception('Value needs to be an array');
}
};

$field->transform = function ($value) {
if (empty($value) || !is_array($value)) {
return array();
}
$withValues = array();
foreach ($value as $dim) {
if (!empty($dim['index']) && !empty($dim['value'])) {
$withValues[] = $dim;
}
}

return $withValues;
};

$field->uiControl = FieldConfig::UI_CONTROL_MULTI_TUPLE;
$field1 = new FieldConfig\MultiPair('Index', 'index', FieldConfig::UI_CONTROL_TEXT);
$field1->customUiControlTemplateFile = self::FIELD_TEMPLATE_VARIABLE;
$field2 = new FieldConfig\MultiPair('Value', 'value', FieldConfig::UI_CONTROL_TEXT);
$field2->customUiControlTemplateFile = self::FIELD_TEMPLATE_VARIABLE;
$field->uiControlAttributes['field1'] = $field1->toArray();
$field->uiControlAttributes['field2'] = $field2->toArray();
}),
$this->makeSetting('customVariables', array(), FieldConfig::TYPE_ARRAY, function (FieldConfig $field) {
$field->title = 'Custom Variables';
$field->description = 'Optionally set one or multiple custom variables. Scope "action" will set the scope based on the selected tracking type.';
$field->validate = function ($value) {
if (empty($value)) {
return;
}
if (!is_array($value)) {
throw new \Exception('Value needs to be an array');
}
};

$field->transform = function ($value) {
if (empty($value) || !is_array($value)) {
return array();
}
$withValues = array();
foreach ($value as $var) {
if (!empty($var['index']) && !empty($var['name']) && !empty($var['value']) && !empty($var['scope'])) {
$withValues[] = $var;
}
}

return $withValues;
};

$field->uiControl = FieldConfig::UI_CONTROL_MULTI_TUPLE;
$field1 = new FieldConfig\MultiPair('Index', 'index', FieldConfig::UI_CONTROL_TEXT);
$field1->customUiControlTemplateFile = self::FIELD_TEMPLATE_VARIABLE;
$field2 = new FieldConfig\MultiPair('Name', 'name', FieldConfig::UI_CONTROL_TEXT);
$field2->customUiControlTemplateFile = self::FIELD_TEMPLATE_VARIABLE;
$field3 = new FieldConfig\MultiPair('Value', 'value', FieldConfig::UI_CONTROL_TEXT);
$field3->customUiControlTemplateFile = self::FIELD_TEMPLATE_VARIABLE;
$field4 = new FieldConfig\MultiPair('Scope', 'scope', FieldConfig::UI_CONTROL_SINGLE_SELECT);
$field4->availableValues = array(
'visit' => 'visit',
'action' => 'action',
);
$field->uiControlAttributes['field1'] = $field1->toArray();
$field->uiControlAttributes['field2'] = $field2->toArray();
$field->uiControlAttributes['field3'] = $field3->toArray();
$field->uiControlAttributes['field4'] = $field4->toArray();
}),
);
}

Expand Down
49 changes: 48 additions & 1 deletion Template/Tag/MatomoTag.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,55 @@
}
}

if (matomoConfig.customVariables
&& TagManager.utils.isArray(matomoConfig.customVariables)
&& matomoConfig.customVariables.length) {
var varIndex;
for (varIndex = 0; varIndex < matomoConfig.customVariables.length; varIndex++) {
var variable = matomoConfig.customVariables[varIndex];
if (variable && TagManager.utils.isObject(variable) && variable.index && variable.name && variable.value && variable.scope) {
tracker.setCustomVariable(variable.index, variable.name, variable.value, variable.scope);
}
}
}

if (tracker) {
var trackingType = parameters.get('trackingType');
var trackingType = parameters.get('trackingType'),
customDimensions = parameters.get('customDimensions'),
customVariables = parameters.get('customVariables');

if (customDimensions
&& TagManager.utils.isArray(customDimensions)
&& customDimensions.length) {
var dimIndex;
for (dimIndex = 0; dimIndex < customDimensions.length; dimIndex++) {
var dimension = customDimensions[dimIndex];
if (dimension && TagManager.utils.isObject(dimension) && dimension.index && dimension.value) {
tracker.setCustomDimension(dimension.index, dimension.value);
}
}
}

if (customVariables
&& TagManager.utils.isArray(customVariables)
&& customVariables.length) {
var varIndex;
for (varIndex = 0; varIndex < customVariables.length; varIndex++) {
var variable = customVariables[varIndex];
if (variable && TagManager.utils.isObject(variable) && variable.index && variable.name && variable.value && variable.scope) {

if (variable.scope === 'action') {
if (trackingType === 'pageview') {
variable.scope = 'page';
} else {
variable.scope = 'event';
}
}

tracker.setCustomVariable(variable.index, variable.name, variable.value, variable.scope);
}
}
}

if (trackingType === 'pageview') {
var customTitle = parameters.get('documentTitle');
Expand Down
44 changes: 44 additions & 0 deletions Template/Variable/MatomoConfigurationVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,50 @@ public function getParameters()
$field->uiControlAttributes['field1'] = $field1->toArray();
$field->uiControlAttributes['field2'] = $field2->toArray();
}),
$this->makeSetting('customVariables', array(), FieldConfig::TYPE_ARRAY, function (FieldConfig $field) {
$field->title = 'Custom Variables';
$field->description = 'Optionally set one or multiple custom variables.';
$field->validate = function ($value) {
if (empty($value)) {
return;
}
if (!is_array($value)) {
throw new \Exception('Value needs to be an array');
}
};

$field->transform = function ($value) {
if (empty($value) || !is_array($value)) {
return array();
}
$withValues = array();
foreach ($value as $var) {
if (!empty($var['index']) && !empty($var['name']) && !empty($var['value']) && !empty($var['scope'])) {
$withValues[] = $var;
}
}

return $withValues;
};

$field->uiControl = FieldConfig::UI_CONTROL_MULTI_TUPLE;
$field1 = new FieldConfig\MultiPair('Index', 'index', FieldConfig::UI_CONTROL_TEXT);
$field1->customUiControlTemplateFile = self::FIELD_TEMPLATE_VARIABLE;
$field2 = new FieldConfig\MultiPair('Name', 'name', FieldConfig::UI_CONTROL_TEXT);
$field2->customUiControlTemplateFile = self::FIELD_TEMPLATE_VARIABLE;
$field3 = new FieldConfig\MultiPair('Value', 'value', FieldConfig::UI_CONTROL_TEXT);
$field3->customUiControlTemplateFile = self::FIELD_TEMPLATE_VARIABLE;
$field4 = new FieldConfig\MultiPair('Scope', 'scope', FieldConfig::UI_CONTROL_SINGLE_SELECT);
$field4->availableValues = array(
'visit' => 'visit',
'page' => 'page',
'event' => 'event',
);
$field->uiControlAttributes['field1'] = $field1->toArray();
$field->uiControlAttributes['field2'] = $field2->toArray();
$field->uiControlAttributes['field3'] = $field3->toArray();
$field->uiControlAttributes['field4'] = $field4->toArray();
}),
$this->makeSetting('bundleTracker', true, FieldConfig::TYPE_BOOL, function (FieldConfig $field) {
$field->title = 'Bundle Tracker';
$field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX;
Expand Down
2 changes: 1 addition & 1 deletion stylesheets/manageEdit.less
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
max-width: 275px !important;
}

.multiple {
.multiple:not(.has4Fields) {
.innerFormField {
margin-left: -0.75rem;
max-width: 350px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<eventAction />
<eventName />
<eventValue />
<customDimensions />
<customVariables />
</parameters>
<fire_trigger_ids>
<row>33</row>
Expand Down Expand Up @@ -91,6 +93,8 @@
<userId />
<customDimensions>
</customDimensions>
<customVariables>
</customVariables>
<bundleTracker>1</bundleTracker>
<jsEndpoint>matomo.js</jsEndpoint>
<trackingEndpoint>matomo.php</trackingEndpoint>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,38 @@
<introduction />
<condition />
</row>
<row>
<name>customVariables</name>
<title>Custom Variables</title>
<value>
</value>
<defaultValue>
</defaultValue>
<type>array</type>
<uiControl>multituple</uiControl>
<uiControlAttributes>
<field1>
<key>index</key>
<title>Index</title>
<uiControl>text</uiControl>
<templateFile>plugins/TagManager/angularjs/form-field/field-variable-template.html</templateFile>
<availableValues />
</field1>
<field2>
<key>value</key>
<title>Value</title>
<uiControl>text</uiControl>
<templateFile>plugins/TagManager/angularjs/form-field/field-variable-template.html</templateFile>
<availableValues />
</field2>
</uiControlAttributes>
<availableValues />
<description>Optionally set one or multiple custom variables.</description>
<inlineHelp />
<templateFile />
<introduction />
<condition />
</row>
</parameters>
</row>
<row>
Expand Down Expand Up @@ -1003,6 +1035,38 @@
<introduction />
<condition />
</row>
<row>
<name>customVariables</name>
<title>Custom Variables</title>
<value>
</value>
<defaultValue>
</defaultValue>
<type>array</type>
<uiControl>multituple</uiControl>
<uiControlAttributes>
<field1>
<key>index</key>
<title>Index</title>
<uiControl>text</uiControl>
<templateFile>plugins/TagManager/angularjs/form-field/field-variable-template.html</templateFile>
<availableValues />
</field1>
<field2>
<key>value</key>
<title>Value</title>
<uiControl>text</uiControl>
<templateFile>plugins/TagManager/angularjs/form-field/field-variable-template.html</templateFile>
<availableValues />
</field2>
</uiControlAttributes>
<availableValues />
<description>Optionally set one or multiple custom Variables.</description>
<inlineHelp />
<templateFile />
<introduction />
<condition />
</row>
<row>
<name>bundleTracker</name>
<title>Bundle Tracker</title>
Expand Down