-
Notifications
You must be signed in to change notification settings - Fork 58
/
API.php
1536 lines (1362 loc) · 72.8 KB
/
API.php
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;
/**
* API for plugin Tag Manager.
*
* Lets you configure all your containers, create, update and delete tags, triggers, and variables. Create and publish
* new releases, enable and disable preview/debug mode, and much more.
*
* Please note: A container may have several versions. The current version that a user is editing is called the "draft"
* version. You can get the ID of the "draft" version by calling {@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;
}
/**
* Get a list of all available contexts that can be used on this system. For example "web", "android", "ios"
* @return array[]
*/
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;
}
/**
* Get a list of all available environments such as "live", "dev", "staging"
* @return array
*/
public function getAvailableEnvironments()
{
Piwik::checkUserHasSomeViewAccess();
$this->checkUserHasTagManagerAccess();
return $this->environment->getEnvironments();
}
/**
* Get a list of all available environments such as "live", "dev", "staging" with the permission to publish.
*
* @param int $idSite
* @return array
*/
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;
});
}
/**
* Get a list of all available fire limits which can be used when creating or updating a tag.
* @return array
*/
public function getAvailableTagFireLimits()
{
Piwik::checkUserHasSomeViewAccess();
$this->checkUserHasTagManagerAccess();
return $this->tags->getFireLimits();
}
/**
* Get a list of all available comparisons which can be used for example as part of a trigger condition (filter)
* or as part of a variable lookup table.
* @return array
*/
public function getAvailableComparisons()
{
Piwik::checkUserHasSomeViewAccess();
$this->checkUserHasTagManagerAccess();
return $this->comparisons->getSupportedComparisons();
}
/**
* Returns a list of all available tag types in the context (for example "web").
* @param string $idContext The ID of a context, for example "web", "android" or "ios"
* @return array
*/
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 a list of all available trigger types in the context (for example "web").
* @param string $idContext The ID of a context, for example "web", "android" or "ios"
* @return array
*/
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 a list of all available variable types in the context (for example "web").
* @param string $idContext The ID of a context, for example "web", "android" or "ios"
* @return array
*/
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;
}
/**
* Get the HTML/JavaScript block which loads a specific container. This allows you to automatically embed
* a container into your website. It will return an HTML block containing a JavaScript element.
*
* Note: This method currently only works for containers in context "web".
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param string $environment The id of an environment, for example "live"
* @return string
*/
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 instructions on how to embed the given container.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param string $environment The id of an environment, for example "live"
* @param string $jsFramework The jsFramework for which instructions need to be fetched, for example "react"
* @return array[]
*/
public function getContainerInstallInstructions($idSite, $idContainer, $environment, $jsFramework = '')
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerExists($idSite, $idContainer);
return $this->containers->getContainerInstallInstructions($idSite, $idContainer, $environment, $jsFramework);
}
/**
* Get a list of all configured tags within the given container version.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of tags will be different per container. Therefore you need to provide
* the ID of the version.
* @return array
*/
public function getContainerTags($idSite, $idContainer, $idContainerVersion)
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
return $this->tags->getContainerTags($idSite, $idContainerVersion);
}
/**
* Creates the default container for the given site. This container will automatically have a configured tag
* to track a Matomo instance and also have a trigger assigned to track a pageview when a page is being viewed.
*
* While the Tag Manager creates this container by default for all new websites (measurables), it won't create
* this container automatically for all previously existing websites if you have used Matomo before without the
* Tag Manager. This API allows you to easily create this default container for all websites.
*
* Note: If the current site already has a default container, another default container will be created.
*
* @param int $idSite
* @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;
}
}
}
$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 new tag within the given container version.
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of tags will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @param string $type The type of tag you want to create, for example "Matomo".
* @param string $name The name this tag should have
* @param array $parameters A key/value pair to define values for specific parameters. For example array('parameterName' => 'value')
* @param int[] $fireTriggerIds A list of trigger IDs that define when this tag should be fired. A tag will be executed as soon as any of these triggers fire. At least one trigger needs to be set.
* @param int[] $blockTriggerIds Optional, a list of trigger IDs that block the execution of a tag. As soon as any of these triggers have been triggered, the tag will not be executed
* @param string $fireLimit Optional, limit how often the tag will be executed. For a list of available fire limits call {@link TagManager.getAvailableTagFireLimits}
* @param int $fireDelay Optional, a delay in milliseconds. If specified, instead of the tag being executed right away when a fire trigger is being triggered, the execution will be delayed.
* @param int $priority Optional, a custom priority which defines the order in which certain tags will be executed if multiple will be triggered at once. The lower the priority is, the earlier this tag may be fired.
* @param null|string $startDate Optional, a start date to ensure the tag will be only executed after this date. Please provide the date in UTC.
* @param null|string $endDate Optional, an end date to ensure the tag will not be executed after this date. Please provide the date in UTC.
* @param null|string $description Optional description
*
* @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 = $this->decodeQuotes($name);
$this->accessValidator->checkWriteCapability($idSite);
$this->containers->checkContainerVersionExists($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 specific tag configuration.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of tags will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @param int $idTag The id of the tag you want to update.
* @param string $name The name this tag should have
* @param array $parameters A key/value pair to define values for specific parameters. For example array('parameterName' => 'value')
* @param int[] $fireTriggerIds A list of trigger IDs that define when this tag should be fired. A tag will be executed as soon as any of these triggers fire. At least one trigger needs to be set.
* @param int[] $blockTriggerIds Optional, a list of trigger IDs that block the execution of a tag. As soon as any of these triggers have been triggered, the tag will not be executed
* @param string $fireLimit Optional, limit how often the tag will be executed. For a list of available fire limits call {@link TagManager.getAvailableTagFireLimits}
* @param int $fireDelay Optional, a delay in milliseconds. If specified, instead of the tag being executed right away when a fire trigger is being triggered, the execution will be delayed.
* @param int $priority Optional, a custom priority which defines the order in which certain tags will be executed if multiple will be triggered at once. The lower the priority is, the earlier this tag may be fired.
* @param null|string $startDate Optional, a start date to ensure the tag will be only executed after this date. Please provide the date in UTC.
* @param null|string $endDate Optional, an end date to ensure the tag will not be executed after this date. Please provide the date in UTC.
* @param null|string $description Optional description
*/
public function updateContainerTag(
$idSite,
$idContainer,
$idContainerVersion,
$idTag,
$name,
$parameters = [],
$fireTriggerIds = [],
$blockTriggerIds = [],
$fireLimit = 'unlimited',
$fireDelay = 0,
$priority = 999,
$startDate = null,
$endDate = null,
$description = ''
) {
$name = $this->decodeQuotes($name);
$this->accessValidator->checkWriteCapability($idSite);
$this->containers->checkContainerVersionExists($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;
}
/**
* Delete (remove) the given tag from the given container version.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of tags will be different per container. Therefore you need to provide
* the ID of the version.
* @param int $idTag The id of the tag you want to delete
*/
public function deleteContainerTag($idSite, $idContainer, $idContainerVersion, $idTag)
{
$this->accessValidator->checkWriteCapability($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
if ($this->getContainerTag($idSite, $idContainer, $idContainerVersion, $idTag)) {
$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
)));
}
}
/**
* Pause the given tag from the given container version.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of tags will be different per container. Therefore you need to provide
* the ID of the version.
* @param int $idTag The id of the tag you want to pause
*/
public function pauseContainerTag($idSite, $idContainer, $idContainerVersion, $idTag)
{
$this->accessValidator->checkWriteCapability($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
if ($this->getContainerTag($idSite, $idContainer, $idContainerVersion, $idTag)) {
$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;
}
/**
* Re-acivate the given tag from the given container version.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of tags will be different per container. Therefore you need to provide
* the ID of the version.
* @param int $idTag The id of the tag you want to re-activate
*/
public function resumeContainerTag($idSite, $idContainer, $idContainerVersion, $idTag)
{
$this->accessValidator->checkWriteCapability($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
if ($this->getContainerTag($idSite, $idContainer, $idContainerVersion, $idTag)) {
$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;
}
/**
* Get a specific tag configuration.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of tags will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @param int $idTag The id of the tag you want to fetch.
* @return array
*/
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 a list of all places where this trigger is being referenced. This would be typically a list of all
* tags that have this trigger in use. A trigger can be only deleted if the trigger is no longer referenced, therefore
* you may need to ensure to first unassign the trigger from all references before deleting a trigger.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of trigger will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @param int $idTrigger The id of the trigger you want to fetch the references for.
* @return array
*/
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;
}
/**
* Get a list of all triggers within a specific container version.
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of trigger will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @return array
*/
public function getContainerTriggers($idSite, $idContainer, $idContainerVersion)
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
return $this->triggers->getContainerTriggers($idSite, $idContainerVersion);
}
/**
* Creates a new trigger within the given container version.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of triggers will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @param int $type The type of trigger you want create, for example "AllElements".
* @param string $name The name this trigger should have
* @param array $parameters A key/value pair to define values for specific parameters. For example array('parameterName' => 'value')
* @param array[] $conditions An array containing one or multiple conditions to filter when a trigger will be triggered. For example:
* array(array('actual' => 'VARIABLENAME', 'comparison' => 'equals', 'expected' => 'expectedValue'))
* To get a list of available comparisons, call {@link TagManager.getAvailableComparisons}
* @param null|string $description Optional description
*
* @return int The id of the created trigger
*/
public function addContainerTrigger($idSite, $idContainer, $idContainerVersion, $type, $name, $parameters = [], $conditions = [], $description = '')
{
$name = $this->decodeQuotes($name);
$this->accessValidator->checkWriteCapability($idSite);
$this->containers->checkContainerVersionExists($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 the configuration of a specific trigger.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of triggers will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @param int $idTrigger The id of the trigger you want to update.
* @param string $name The name this trigger should have
* @param array $parameters A key/value pair to define values for specific parameters. For example array('parameterName' => 'value')
* @param array[] $conditions An array containing one or multiple conditions to filter when a trigger will be triggered. For example:
* array(array('actual' => 'VARIABLENAME', 'comparison' => 'equals', 'expected' => 'expectedValue'))
* To get a list of available comparisons, call {@link TagManager.getAvailableComparisons}
* @param null|string $description Optional description
*/
public function updateContainerTrigger($idSite, $idContainer, $idContainerVersion, $idTrigger, $name, $parameters = [], $conditions = [], $description = '')
{
$name = $this->decodeQuotes($name);
$this->accessValidator->checkWriteCapability($idSite);
$this->containers->checkContainerVersionExists($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;
}
/**
* Delete (remove) the given trigger from the container.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of trigger will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @param int $idTrigger The id of the trigger you want to delete.
*/
public function deleteContainerTrigger($idSite, $idContainer, $idContainerVersion, $idTrigger)
{
$this->accessValidator->checkWriteCapability($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
if ($this->getContainerTrigger($idSite, $idContainer, $idContainerVersion, $idTrigger)) {
$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
)));
}
}
/**
* Get the configuration of a specific trigger.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of trigger will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @param int $idTrigger The id of the trigger you want to get.
* @return array
*/
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 a list of all places where this variable is being referenced. This would be typically a list of all
* tags, triggers, and variables that have this variable in use. A variable can be only deleted if the variable
* is no longer referenced, therefore you may need to ensure to first unassign/remove the variable from all
* references before deleting a variable.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of variable will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @param int $idVariable The id of the variable you want to fetch the references for.
* @return array
*/
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;
}
/**
* Get a list of all manually configured variables within a container version. This API method does not return any preconfigured
* variables. To fetch a list of all configured variables and all pre-configured variables, call
* {@link TagManager.getAvailableContainerVariables}.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of variable will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @return array
*/
public function getContainerVariables($idSite, $idContainer, $idContainerVersion)
{
$this->accessValidator->checkViewPermission($idSite);
$this->containers->checkContainerVersionExists($idSite, $idContainer, $idContainerVersion);
return $this->variables->getContainerVariables($idSite, $idContainerVersion);
}
/**
* Get a list of all manually configured and all preconfigured variables within a container version.
* To fetch a list of only manually configured variables (by a user), call
* {@link TagManager.getContainerVariables}.
*
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of variable will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @return array
*/
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);
}
/**
* @param int $idSite The id of the site the given container belongs to
* @param string $idContainer The id of a container, for example "6OMh6taM"
* @param int $idContainerVersion The ID of the container version, a container may have multiple versions and
* the list of variable will be different per container. Therefore you need to provide
* the ID of the version you are referring to.
* @param string $type The type of variable you want to create.
* @param string $name The name this variable should have
* @param array $parameters A key/value pair to define values for specific parameters. For example array('parameterName' => 'value')
* @param null|string $defaultValue Optionally a default value
* @param array[] $lookupTable An array containing one or multiple lookup configurations. For example:
* array(array('match_value' => 'inval', 'comparison' => 'equals', 'out_value' => 'outval'))
* For a list of available comparisons see {@link TagManager.getAvailableComparisons}
* @param null|string $description Optional description
*
* @return int The ID of the created variable
*/
public function addContainerVariable($idSite, $idContainer, $idContainerVersion, $type, $name, $parameters = [], $defaultValue = false, $lookupTable = [], $description = '')
{
$name = $this->decodeQuotes($name);
$this->accessValidator->checkWriteCapability($idSite);
$this->containers->checkContainerVersionExists($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);
}