-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathAPI.php
More file actions
1568 lines (1393 loc) · 70.5 KB
/
API.php
File metadata and controls
1568 lines (1393 loc) · 70.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\TagManager;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\Piwik;
use Piwik\Plugins\TagManager\Access\Capability\PublishLiveContainer;
use Piwik\Plugins\TagManager\API\Export;
use Piwik\Plugins\TagManager\API\Import;
use Piwik\Plugins\TagManager\API\PreviewCookie;
use Piwik\Plugins\TagManager\API\TemplateMetadata;
use Piwik\Plugins\TagManager\Context\WebContext;
use Piwik\Plugins\TagManager\Dao\BaseDao;
use Piwik\Plugins\TagManager\Dao\ContainersDao;
use Piwik\Plugins\TagManager\Dao\VariablesDao;
use Piwik\Plugins\TagManager\Exception\EntityRecursionException;
use Piwik\Plugins\TagManager\Input\AccessValidator;
use Piwik\Plugins\TagManager\Model\Comparison;
use Piwik\Plugins\TagManager\Model\Container;
use Piwik\Plugins\TagManager\Model\Environment;
use Piwik\Plugins\TagManager\Model\Tag;
use Piwik\Plugins\TagManager\Model\Trigger;
use Piwik\Plugins\TagManager\Model\Variable;
use Piwik\Plugins\TagManager\Context\ContextProvider;
use Piwik\Plugins\TagManager\Template\Tag\MatomoTag;
use Piwik\Plugins\TagManager\Template\Tag\TagsProvider;
use Piwik\Plugins\TagManager\Template\Trigger\PageViewTrigger;
use Piwik\Plugins\TagManager\Template\Trigger\TriggersProvider;
use Piwik\Plugins\TagManager\Template\Variable\MatomoConfigurationVariable;
use Piwik\Plugins\TagManager\Template\Variable\VariablesProvider;
use Exception;
use Piwik\UrlHelper;
use Piwik\Validators\BaseValidator;
use Piwik\Validators\CharacterLength;
use Piwik\Validators\NotEmpty;
/**
* Exposes the Tag Manager API for managing containers, versions, tags, triggers, and variables.
*
* The endpoints also provide installation metadata, publishing workflows, preview controls, and import/export
* operations. A container may have several versions, and the current editable version is exposed as the "draft"
* version by {@link TagManager.getContainer}.
*
* @method static \Piwik\Plugins\TagManager\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
/**
* @var Tag
*/
private $tags;
/**
* @var Trigger
*/
private $triggers;
/**
* @var Variable
*/
private $variables;
/**
* @var Container
*/
private $containers;
/**
* @var TagsProvider
*/
private $tagsProvider;
/**
* @var TriggersProvider
*/
private $triggersProvider;
/**
* @var VariablesProvider
*/
private $variablesProvider;
/**
* @var ContextProvider
*/
private $contextProvider;
/**
* @var Environment
*/
private $environment;
/**
* @var Comparison
*/
private $comparisons;
/**
* @var AccessValidator
*/
private $accessValidator;
/**
* @var Export
*/
private $export;
/**
* @var Import
*/
private $import;
/**
* @var VariablesDao
*/
private $variablesDao;
private $enableGeneratePreview = true;
public function __construct(
Tag $tags,
Trigger $triggers,
Variable $variables,
Container $containers,
TagsProvider $tagsProvider,
TriggersProvider $triggersProvider,
VariablesProvider $variablesProvider,
ContextProvider $contextProvider,
AccessValidator $validator,
Environment $environment,
Comparison $comparisons,
Export $export,
Import $import,
VariablesDao $variablesDao
) {
//Started updating xdebug.max_nesting_level as infinite loop is detected due to variable is doing a self referencing when xdebug is active and max_nesting_level is set to lower value
if (extension_loaded('xdebug')) {
$xdebugMaxNestingLevel = ini_get('xdebug.max_nesting_level');
if ($xdebugMaxNestingLevel && is_numeric($xdebugMaxNestingLevel) && $xdebugMaxNestingLevel < 2500) {
ini_set('xdebug.max_nesting_level', 2500);
}
}
$this->tags = $tags;
$this->triggers = $triggers;
$this->variables = $variables;
$this->containers = $containers;
$this->tagsProvider = $tagsProvider;
$this->triggersProvider = $triggersProvider;
$this->variablesProvider = $variablesProvider;
$this->contextProvider = $contextProvider;
$this->environment = $environment;
$this->accessValidator = $validator;
$this->export = $export;
$this->import = $import;
$this->comparisons = $comparisons;
$this->variablesDao = $variablesDao;
}
/**
* Returns the contexts that currently expose at least one tag type.
*
* @return array<int, array<string, mixed>> The available context definitions, such as "web", "android", or
* "ios".
*/
public function getAvailableContexts()
{
Piwik::checkUserHasSomeViewAccess();
$this->checkUserHasTagManagerAccess();
$contexts = $this->contextProvider->getAllContexts();
$return = array();
foreach ($contexts as $context) {
$tags = $this->getAvailableTagTypesInContext($context->getId());
if (!empty($tags)) {
$return[] = $context->toArray();
}
}
return $return;
}
/**
* Returns the environments that can receive container releases.
*
* @return array<int, array<string, mixed>> The configured environments, such as "live", "dev", and "staging".
*/
public function getAvailableEnvironments()
{
Piwik::checkUserHasSomeViewAccess();
$this->checkUserHasTagManagerAccess();
return $this->environment->getEnvironments();
}
/**
* Returns the environments the current user can publish to for a site.
*
* @param int $idSite The numeric ID of the website to query.
* @return array<int, array<string, mixed>> The publishable environments for the site, excluding "live" when the
* user lacks that capability.
*/
public function getAvailableEnvironmentsWithPublishCapability($idSite)
{
Piwik::checkUserHasSomeViewAccess();
$this->checkUserHasTagManagerAccess($idSite);
$environments = $this->environment->getEnvironments();
$hasCapability = $this->accessValidator->hasPublishLiveEnvironmentCapability($idSite);
return array_filter($environments, function ($environment) use ($idSite, $hasCapability) {
if ($environment['id'] === 'live' && !$hasCapability) {
return false;
}
return true;
});
}
/**
* Returns the supported fire limit options for tags.
*
* @return array<int, array<string, mixed>> The available fire limit IDs and translated labels.
*/
public function getAvailableTagFireLimits()
{
Piwik::checkUserHasSomeViewAccess();
$this->checkUserHasTagManagerAccess();
return $this->tags->getFireLimits();
}
/**
* Returns the comparison operators available for trigger conditions and variable lookup tables.
*
* @return array<int, array<string, mixed>> The supported comparison definitions.
*/
public function getAvailableComparisons()
{
Piwik::checkUserHasSomeViewAccess();
$this->checkUserHasTagManagerAccess();
return $this->comparisons->getSupportedComparisons();
}
/**
* Returns the tag templates that support the requested context.
*
* @param string $idContext The context ID to query, for example "web", "android", or "ios".
* @return array<int, array<string, mixed>> The tag template metadata available in the context.
*/
public function getAvailableTagTypesInContext($idContext)
{
Piwik::checkUserHasSomeViewAccess();
$this->checkUserHasTagManagerAccess();
$this->contextProvider->checkIsValidContext($idContext);
$tags = $this->tagsProvider->getAllTags();
$tagsInContext = [];
foreach ($tags as $tag) {
// GA3 tag deprecated
if ($tag->getId() === 'GoogleAnalyticsUniversal') {
continue;
}
if (in_array($idContext, $tag->getSupportedContexts(), true)) {
$tagsInContext[] = $tag;
}
}
$templateMetadata = new TemplateMetadata();
return $templateMetadata->formatTemplates($tagsInContext);
}
/**
* Returns the trigger templates that support the requested context.
*
* @param string $idContext The context ID to query, for example "web", "android", or "ios".
* @return array<int, array<string, mixed>> The trigger template metadata available in the context.
*/
public function getAvailableTriggerTypesInContext($idContext)
{
Piwik::checkUserHasSomeViewAccess();
$this->checkUserHasTagManagerAccess();
$this->contextProvider->checkIsValidContext($idContext);
$triggers = $this->triggersProvider->getAllTriggers();
$triggersInContext = [];
foreach ($triggers as $trigger) {
if (in_array($idContext, $trigger->getSupportedContexts(), true)) {
$triggersInContext[] = $trigger;
}
}
$templateMetadata = new TemplateMetadata();
return $templateMetadata->formatTemplates($triggersInContext);
}
/**
* Returns the manually creatable variable templates that support the requested context.
*
* @param string $idContext The context ID to query, for example "web", "android", or "ios".
* @return array<int, array<string, mixed>> The variable template metadata available in the context, excluding
* preconfigured variables.
*/
public function getAvailableVariableTypesInContext($idContext)
{
Piwik::checkUserHasSomeViewAccess();
$this->checkUserHasTagManagerAccess();
$this->contextProvider->checkIsValidContext($idContext);
$variables = $this->variablesProvider->getAllVariables();
$variablesInContext = [];
foreach ($variables as $variable) {
if (!$variable->isPreConfigured() && in_array($idContext, $variable->getSupportedContexts(), true)) {
$variablesInContext[] = $variable;
}
}
$templateMetadata = new TemplateMetadata();
return $templateMetadata->formatTemplates($variablesInContext);
}
private function unsanitizeAssocArray($parameters)
{
if (!empty($parameters) && is_array($parameters)) {
foreach ($parameters as $index => $value) {
if (is_string($value)) {
$parameters[$index] = Common::unsanitizeInputValue($value);
} elseif (is_array($value)) {
$parameters[$index] = $this->unsanitizeAssocArray($value);
}
}
}
return $parameters;
}
/**
* Returns the embed code for loading a container release on a website.
*
* This endpoint currently supports only containers in the "web" context.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param string $environment The environment ID to load, for example "live".
* @return string The HTML and JavaScript embed snippet for the requested container release.
*/
public function getContainerEmbedCode($idSite, $idContainer, $environment)
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerExists($idSite, $idContainer);
$instructions = $this->containers->getContainerInstallInstructions($idSite, $idContainer, $environment);
$instruction = array_shift($instructions);
return $instruction['embedCode'];
}
/**
* Returns installation instructions for embedding a container release.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param string $environment The environment ID to load, for example "live".
* @param string $jsFramework The JavaScript framework variant to return, for example "react".
* @return array<int, array<string, mixed>> The install instruction steps and metadata for the requested
* container release.
*/
public function getContainerInstallInstructions($idSite, $idContainer, $environment, $jsFramework = '')
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerExists($idSite, $idContainer);
return $this->containers->getContainerInstallInstructions($idSite, $idContainer, $environment, $jsFramework);
}
/**
* Returns all configured tags in a container version.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to inspect.
* @return array<int, array<string, mixed>> The configured tags in the requested container version.
*/
public function getContainerTags($idSite, $idContainer, $idContainerVersion)
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
return $this->tags->getContainerTags($idSite, $idContainerVersion);
}
/**
* Creates a default web container for a site with the standard Matomo tracking setup.
*
* The created draft includes a Matomo configuration variable, a page view trigger, an initial Matomo tag, and a
* first published live release.
*
* @param int $idSite The numeric ID of the website to query.
* @return string The ID of the created container.
*/
public function createDefaultContainerForSite($idSite)
{
$this->accessValidator->checkWriteCapability($idSite);
$loop = 0;
$idContainer = null;
while (empty($idContainer) && $loop <= 50) {
// we try up to 51 times whether a name is available, and otherwise we give up
$name = Piwik::translate('TagManager_DefaultContainer');
if ($loop > 0) {
$name .= ' ' . $loop;
}
try {
$idContainer = Request::processRequest('TagManager.addContainer', array(
'idSite' => $idSite,
'context' => WebContext::ID,
'name' => $name,
'description' => Piwik::translate('TagManager_AutoGeneratedContainerDescription')
), $default = []);
} catch (Exception $e) {
if ($e->getCode() !== ContainersDao::ERROR_NAME_IN_USE || $loop === 50) {
throw $e;
}
$loop++;
}
}
$draftVersion = $this->getContainerDraftVersion($idSite, $idContainer);
$idVariable = Request::processRequest('TagManager.addContainerVariable', array(
'idSite' => $idSite,
'idContainer' => $idContainer,
'idContainerVersion' => $draftVersion,
'type' => MatomoConfigurationVariable::ID,
'name' => Piwik::translate('TagManager_MatomoConfigurationVariableName'),
), $default = []);
$idTrigger = Request::processRequest('TagManager.addContainerTrigger', array(
'idSite' => $idSite,
'idContainer' => $idContainer,
'idContainerVersion' => $draftVersion,
'type' => PageViewTrigger::ID,
'name' => Piwik::translate('TagManager_PageViewTriggerName'),
), $default = []);
$idTag = Request::processRequest('TagManager.addContainerTag', array(
'idSite' => $idSite,
'idContainer' => $idContainer,
'idContainerVersion' => $draftVersion,
'type' => MatomoTag::ID,
'name' => Piwik::translate('TagManager_MatomoTagName'),
'fireTriggerIds' => array($idTrigger),
'parameters' => array(
MatomoTag::PARAM_MATOMO_CONFIG => '{{' . Piwik::translate('TagManager_MatomoConfigurationVariableName') . '}}'
)
), $default = []);
Request::processRequest('TagManager.createContainerVersion', array(
'idSite' => $idSite,
'idContainer' => $idContainer,
'name' => substr('0.1.0 - ' . Piwik::translate('TagManager_AutoGenerated'), 0, 50),
), $default = []);
Request::processRequest('TagManager.publishContainerVersion', array(
'idSite' => $idSite,
'idContainer' => $idContainer,
'idContainerVersion' => $draftVersion,
'environment' => Environment::ENVIRONMENT_LIVE,
), $default = []);
return $idContainer;
}
/**
* Creates a tag in the requested container version.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to modify.
* @param string $type The tag template ID to create, for example "Matomo".
* @param string $name The display name for the tag.
* @param array<string, mixed> $parameters Parameter values keyed by template parameter name.
* @param int[] $fireTriggerIds Trigger IDs that cause the tag to fire. At least one trigger must be provided.
* @param int[] $blockTriggerIds Trigger IDs that prevent the tag from firing after they match.
* @param string $fireLimit Fire limit ID to apply to the tag. Use
* {@link TagManager.getAvailableTagFireLimits} to list supported values.
* @param int $fireDelay Delay in milliseconds before the tag executes after a fire trigger matches.
* @param int $priority Execution priority for the tag. Lower values run earlier when multiple tags fire together.
* @param string|null $startDate UTC datetime after which the tag may execute.
* @param string|null $endDate UTC datetime after which the tag must no longer execute.
* @param string|null $description Optional tag description.
* @param string $status Optional initial status for the created tag.
* @return int The ID of the created tag.
*/
public function addContainerTag(
$idSite,
$idContainer,
$idContainerVersion,
$type,
$name,
$parameters = [],
$fireTriggerIds = [],
$blockTriggerIds = [],
$fireLimit = 'unlimited',
$fireDelay = 0,
$priority = 999,
$startDate = null,
$endDate = null,
$description = '',
$status = ''
) {
$name = trim($this->decodeQuotes($name));
$this->accessValidator->checkWriteCapability($idSite);
$this->assertUserCanEditContainerVersion($idSite, $idContainer, $idContainerVersion);
if ($this->tagsProvider->isCustomTemplate($type) && !Piwik::isUserHasCapability($idSite, PublishLiveContainer::ID)) {
$this->accessValidator->checkUseCustomTemplatesCapability($idSite);
}
$parameters = $this->unsanitizeAssocArray($parameters);
$idTag = $this->tags->addContainerTag($idSite, $idContainerVersion, $type, $name, $parameters, $fireTriggerIds, $blockTriggerIds, $fireLimit, $fireDelay, $priority, $startDate, $endDate, $description, $status);
$this->updateContainerPreviewRelease($idSite, $idContainer);
return $idTag;
}
/**
* Updates a tag in the requested container version.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to modify.
* @param int $idTag The tag ID to update.
* @param string $name The updated display name for the tag.
* @param array<string, mixed> $parameters Parameter values keyed by template parameter name.
* @param int[] $fireTriggerIds Trigger IDs that cause the tag to fire. At least one trigger must be provided.
* @param int[] $blockTriggerIds Trigger IDs that prevent the tag from firing after they match.
* @param string $fireLimit Fire limit ID to apply to the tag. Use
* {@link TagManager.getAvailableTagFireLimits} to list supported values.
* @param int $fireDelay Delay in milliseconds before the tag executes after a fire trigger matches.
* @param int $priority Execution priority for the tag. Lower values run earlier when multiple tags fire together.
* @param string|null $startDate UTC datetime after which the tag may execute.
* @param string|null $endDate UTC datetime after which the tag must no longer execute.
* @param string|null $description Optional tag description.
* @return void
*/
public function updateContainerTag(
$idSite,
$idContainer,
$idContainerVersion,
$idTag,
$name,
$parameters = [],
$fireTriggerIds = [],
$blockTriggerIds = [],
$fireLimit = 'unlimited',
$fireDelay = 0,
$priority = 999,
$startDate = null,
$endDate = null,
$description = ''
) {
$name = trim($this->decodeQuotes($name));
$this->accessValidator->checkWriteCapability($idSite);
$this->assertUserCanEditContainerVersion($idSite, $idContainer, $idContainerVersion);
$tag = $this->tags->getContainerTag($idSite, $idContainerVersion, $idTag);
if (!empty($tag) && $this->tagsProvider->isCustomTemplate($tag['type'])) {
$this->accessValidator->checkUseCustomTemplatesCapability($idSite);
}
$parameters = $this->unsanitizeAssocArray($parameters);
$return = $this->tags->updateContainerTag($idSite, $idContainerVersion, $idTag, $name, $parameters, $fireTriggerIds, $blockTriggerIds, $fireLimit, $fireDelay, $priority, $startDate, $endDate, $description);
$this->updateContainerPreviewRelease($idSite, $idContainer);
return $return;
}
/**
* Deletes a tag from a container version.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to modify.
* @param int $idTag The tag ID to delete.
* @return void
*/
public function deleteContainerTag($idSite, $idContainer, $idContainerVersion, $idTag)
{
$this->accessValidator->checkWriteCapability($idSite);
$this->assertUserCanEditContainerVersion($idSite, $idContainer, $idContainerVersion);
$tag = $this->getContainerTag($idSite, $idContainer, $idContainerVersion, $idTag);
if ($tag) {
if ($this->tagsProvider->isCustomTemplate($tag['type'])) {
$this->accessValidator->checkUseCustomTemplatesCapability($idSite);
}
$this->tags->deleteContainerTag($idSite, $idContainerVersion, $idTag);
$this->updateContainerPreviewRelease($idSite, $idContainer);
Piwik::postEvent('TagManager.deleteContainerTag.end', array(array(
'idSite' => $idSite,
'idContainer' => $idContainer,
'idContainerVersion' => $idContainerVersion,
'idTag' => $idTag
)));
}
}
/**
* Pauses a tag in a container version.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to modify.
* @param int $idTag The tag ID to pause.
* @return bool Returns `true` when the tag was found and paused, or `false` when no matching tag exists.
*/
public function pauseContainerTag($idSite, $idContainer, $idContainerVersion, $idTag)
{
$this->accessValidator->checkWriteCapability($idSite);
$this->assertUserCanEditContainerVersion($idSite, $idContainer, $idContainerVersion);
$tag = $this->getContainerTag($idSite, $idContainer, $idContainerVersion, $idTag);
if ($tag) {
if ($this->tagsProvider->isCustomTemplate($tag['type'])) {
$this->accessValidator->checkUseCustomTemplatesCapability($idSite);
}
$this->tags->pauseContainerTag($idSite, $idContainerVersion, $idTag);
$this->updateContainerPreviewRelease($idSite, $idContainer);
Piwik::postEvent('TagManager.pauseContainerTag.end', array(array(
'idSite' => $idSite,
'idContainer' => $idContainer,
'idContainerVersion' => $idContainerVersion,
'idTag' => $idTag
)));
return true;
}
return false;
}
/**
* Resumes a paused tag in a container version.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to modify.
* @param int $idTag The tag ID to resume.
* @return bool Returns `true` when the tag was found and resumed, or `false` when no matching tag exists.
*/
public function resumeContainerTag($idSite, $idContainer, $idContainerVersion, $idTag)
{
$this->accessValidator->checkWriteCapability($idSite);
$this->assertUserCanEditContainerVersion($idSite, $idContainer, $idContainerVersion);
$tag = $this->getContainerTag($idSite, $idContainer, $idContainerVersion, $idTag);
if ($tag) {
if ($this->tagsProvider->isCustomTemplate($tag['type'])) {
$this->accessValidator->checkUseCustomTemplatesCapability($idSite);
}
$this->tags->resumeContainerTag($idSite, $idContainerVersion, $idTag);
$this->updateContainerPreviewRelease($idSite, $idContainer);
Piwik::postEvent('TagManager.resumeContainerTag.end', array(array(
'idSite' => $idSite,
'idContainer' => $idContainer,
'idContainerVersion' => $idContainerVersion,
'idTag' => $idTag
)));
return true;
}
return false;
}
/**
* Returns the configuration for a specific tag.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to inspect.
* @param int $idTag The tag ID to fetch.
* @return array<string, mixed>|false The tag configuration, or `false` when the tag does not exist in the
* version.
*/
public function getContainerTag($idSite, $idContainer, $idContainerVersion, $idTag)
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
return $this->tags->getContainerTag($idSite, $idContainerVersion, $idTag);
}
/**
* Returns the places where a trigger is referenced in a container version.
*
* A trigger must no longer be referenced before it can be deleted.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to inspect.
* @param int $idTrigger The trigger ID to inspect.
* @return array<int, array<string, mixed>> The tag references that currently use the trigger.
*/
public function getContainerTriggerReferences($idSite, $idContainer, $idContainerVersion, $idTrigger)
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
$references = $this->triggers->getTriggerReferences($idSite, $idContainerVersion, $idTrigger);
return $references;
}
/**
* Returns all configured triggers in a container version.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to inspect.
* @return array<int, array<string, mixed>> The configured triggers in the requested container version.
*/
public function getContainerTriggers($idSite, $idContainer, $idContainerVersion)
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
return $this->triggers->getContainerTriggers($idSite, $idContainerVersion);
}
/**
* Creates a trigger in the requested container version.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to modify.
* @param string $type The trigger template ID to create, for example "AllElements".
* @param string $name The display name for the trigger.
* @param array<string, mixed> $parameters Parameter values keyed by template parameter name.
* @param array<int, array<string, mixed>> $conditions Trigger conditions that determine when the trigger matches.
* Use {@link TagManager.getAvailableComparisons} to list
* supported comparison operators.
* @param string|null $description Optional trigger description.
* @return int The ID of the created trigger.
*/
public function addContainerTrigger($idSite, $idContainer, $idContainerVersion, $type, $name, $parameters = [], $conditions = [], $description = '')
{
$name = trim($this->decodeQuotes($name));
$this->accessValidator->checkWriteCapability($idSite);
$this->assertUserCanEditContainerVersion($idSite, $idContainer, $idContainerVersion);
if ($this->triggersProvider->isCustomTemplate($type) && !Piwik::isUserHasCapability($idSite, PublishLiveContainer::ID)) {
$this->accessValidator->checkUseCustomTemplatesCapability($idSite);
}
$parameters = $this->unsanitizeAssocArray($parameters);
$conditions = $this->unsanitizeAssocArray($conditions);
$idTrigger = $this->triggers->addContainerTrigger($idSite, $idContainerVersion, $type, $name, $parameters, $conditions, $description);
$this->updateContainerPreviewRelease($idSite, $idContainer);
return $idTrigger;
}
/**
* Updates a trigger in the requested container version.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to modify.
* @param int $idTrigger The trigger ID to update.
* @param string $name The updated display name for the trigger.
* @param array<string, mixed> $parameters Parameter values keyed by template parameter name.
* @param array<int, array<string, mixed>> $conditions Trigger conditions that determine when the trigger matches.
* Use {@link TagManager.getAvailableComparisons} to list
* supported comparison operators.
* @param string|null $description Optional trigger description.
* @return void
*/
public function updateContainerTrigger($idSite, $idContainer, $idContainerVersion, $idTrigger, $name, $parameters = [], $conditions = [], $description = '')
{
$name = trim($this->decodeQuotes($name));
$this->accessValidator->checkWriteCapability($idSite);
$this->assertUserCanEditContainerVersion($idSite, $idContainer, $idContainerVersion);
$trigger = $this->triggers->getContainerTrigger($idSite, $idContainerVersion, $idTrigger);
if (!empty($trigger) && $this->triggersProvider->isCustomTemplate($trigger['type'])) {
$this->accessValidator->checkUseCustomTemplatesCapability($idSite);
}
$parameters = $this->unsanitizeAssocArray($parameters);
$conditions = $this->unsanitizeAssocArray($conditions);
$return = $this->triggers->updateContainerTrigger($idSite, $idContainerVersion, $idTrigger, $name, $parameters, $conditions, $description);
$this->updateContainerPreviewRelease($idSite, $idContainer);
return $return;
}
/**
* Deletes a trigger from a container version.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to modify.
* @param int $idTrigger The trigger ID to delete.
* @return void
*/
public function deleteContainerTrigger($idSite, $idContainer, $idContainerVersion, $idTrigger)
{
$this->accessValidator->checkWriteCapability($idSite);
$this->assertUserCanEditContainerVersion($idSite, $idContainer, $idContainerVersion);
$trigger = $this->getContainerTrigger($idSite, $idContainer, $idContainerVersion, $idTrigger);
if ($trigger) {
if ($this->triggersProvider->isCustomTemplate($trigger['type'])) {
$this->accessValidator->checkUseCustomTemplatesCapability($idSite);
}
$this->triggers->deleteContainerTrigger($idSite, $idContainerVersion, $idTrigger);
$this->updateContainerPreviewRelease($idSite, $idContainer);
Piwik::postEvent('TagManager.deleteContainerTrigger.end', array(array(
'idSite' => $idSite,
'idContainer' => $idContainer,
'idContainerVersion' => $idContainerVersion,
'idTrigger' => $idTrigger
)));
}
}
/**
* Returns the configuration for a specific trigger.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to inspect.
* @param int $idTrigger The trigger ID to fetch.
* @return array<string, mixed>|false The trigger configuration, or `false` when the trigger does not exist in the
* version.
*/
public function getContainerTrigger($idSite, $idContainer, $idContainerVersion, $idTrigger)
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
return $this->triggers->getContainerTrigger($idSite, $idContainerVersion, $idTrigger);
}
/**
* Returns the places where a variable is referenced in a container version.
*
* A variable must no longer be referenced before it can be deleted.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to inspect.
* @param int $idVariable The variable ID to inspect.
* @return array<int, array<string, mixed>> The tag, trigger, and variable references that currently use the
* variable.
*/
public function getContainerVariableReferences($idSite, $idContainer, $idContainerVersion, $idVariable)
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
$references = $this->variables->getContainerVariableReferences($idSite, $idContainerVersion, $idVariable);
return $references;
}
/**
* Returns the manually configured variables in a container version.
*
* This endpoint excludes preconfigured variables. Use {@link TagManager.getAvailableContainerVariables} to fetch
* both manual and preconfigured variables together.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to inspect.
* @return array<int, array<string, mixed>> The manually configured variables in the requested container version.
*/
public function getContainerVariables($idSite, $idContainer, $idContainerVersion)
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
return $this->variables->getContainerVariables($idSite, $idContainerVersion);
}
/**
* Returns the manual and preconfigured variables available in a container version.
*
* Use {@link TagManager.getContainerVariables} to fetch only variables created by users.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to inspect.
* @return array<int, array<string, mixed>> The formatted variable metadata available in the container version.
*/
public function getAvailableContainerVariables($idSite, $idContainer, $idContainerVersion)
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
$variables = $this->getContainerVariables($idSite, $idContainer, $idContainerVersion);
$containerVars = [];
foreach ($variables as $index => $variable) {
$containerVars[] = [
'id' => $variable['name'],
'idvariable' => $variable['idvariable'],
'type' => $variable['type'],
'name' => $variable['name'],
'category' => 'Custom',
'description' => '',
'order' => $index,
'is_pre_configured' => false];
}
foreach ($this->variablesProvider->getPreConfiguredVariables() as $variable) {
$containerVars[] = [
'id' => $variable->getId(),
'idvariable' => '',
'type' => $variable->getId(),
'name' => $variable->getName(),
'category' => Piwik::translate($variable->getCategory()),
'description' => $variable->getDescription(),
'order' => $variable->getOrder(),
'is_pre_configured' => true
];
}
$metadata = new TemplateMetadata();
return $metadata->formatTemplates($containerVars);
}
/**
* Creates a variable in the requested container version.
*
* @param int $idSite The numeric ID of the website to query.
* @param string $idContainer The container ID, for example "6OMh6taM".
* @param int $idContainerVersion The container version ID to modify.
* @param string $type The variable template ID to create.
* @param string $name The display name for the variable.
* @param array<string, mixed> $parameters Parameter values keyed by template parameter name.
* @param bool|float|int|string|null $defaultValue Optional fallback value returned when the variable resolves to
* no value.
* @param array<int, array<string, mixed>> $lookupTable Lookup rules for variable value translation. Use
* {@link TagManager.getAvailableComparisons} to list
* supported comparison operators.
* @param string|null $description Optional variable description.
* @return int The ID of the created variable.
*/
public function addContainerVariable($idSite, $idContainer, $idContainerVersion, $type, $name, $parameters = [], $defaultValue = false, $lookupTable = [], $description = '')
{
$name = trim($this->decodeQuotes($name));
$this->accessValidator->checkWriteCapability($idSite);
$this->assertUserCanEditContainerVersion($idSite, $idContainer, $idContainerVersion);
if ($this->variablesProvider->isCustomTemplate($type) && !Piwik::isUserHasCapability($idSite, PublishLiveContainer::ID)) {
$this->accessValidator->checkUseCustomTemplatesCapability($idSite);
}
$parameters = $this->unsanitizeAssocArray($parameters);
$lookupTable = $this->unsanitizeAssocArray($lookupTable);
$name = urldecode($name);
$idVariable = $this->variables->addContainerVariable($idSite, $idContainerVersion, $type, $name, $parameters, $defaultValue, $lookupTable, $description);
try {
$this->updateContainerPreviewRelease($idSite, $idContainer);
} catch (EntityRecursionException $e) {
// we need to delete the previously added variable.... we first have to add the variable to be able to
// detect recursion and simulate container generation... if it fails we delete it again
$this->forceDeleteVariable($idSite, $idContainerVersion, $idVariable);
$this->updateContainerPreviewRelease($idSite, $idContainer);
throw $e;
}
return $idVariable;
}
private function forceDeleteVariable($idSite, $idContainerVersion, $idVariable)
{
// we cannot use model here because it would trigger an error when a variable references itself
// that the variable cannot be deleted because it's still in use by another variable
$now = Date::now()->getDatetime();
$this->variablesDao->deleteContainerVariable($idSite, $idContainerVersion, $idVariable, $now);
}