Skip to content

Commit

Permalink
Merge pull request #710 from matomo-org/pg-2939-ignore-gtm-data-layer…
Browse files Browse the repository at this point in the history
…-setting

Add setting to allow ignoring GTM dataLayer values
  • Loading branch information
snake14 authored Nov 8, 2023
2 parents 41db3b5 + a2e17c9 commit 7a28e88
Show file tree
Hide file tree
Showing 51 changed files with 364 additions and 111 deletions.
10 changes: 6 additions & 4 deletions API.php
Original file line number Diff line number Diff line change
Expand Up @@ -981,12 +981,13 @@ public function getContainers($idSite)
* {@link TagManager.getAvailableContexts}
* @param string $name The name this container should have.
* @param string $description Optionally a description for this container
* @param int $ignoreGtmDataLayer Optionally indicate that we should ignore GTM dataLayer values
* @return string The ID of the created container.
*/
public function addContainer($idSite, $context, $name, $description = '')
public function addContainer($idSite, $context, $name, $description = '', $ignoreGtmDataLayer = 0)
{
$this->accessValidator->checkWriteCapability($idSite);
return $this->containers->addContainer($idSite, $context, $name, $description);
return $this->containers->addContainer($idSite, $context, $name, $description, $ignoreGtmDataLayer);
}

/**
Expand All @@ -996,14 +997,15 @@ public function addContainer($idSite, $context, $name, $description = '')
* @param string $idContainer The ID of the container you want to update, for example "6OMh6taM".
* @param string $name The name this container should have.
* @param string $description Optionally a description for this container.
* @param int $ignoreGtmDataLayer Optionally indicate that we should ignore GTM dataLayer values
* @return string The ID of the created container.
*/
public function updateContainer($idSite, $idContainer, $name, $description = '')
public function updateContainer($idSite, $idContainer, $name, $description = '', $ignoreGtmDataLayer = 0)
{
$this->accessValidator->checkWriteCapability($idSite);
$this->containers->checkContainerExists($idSite, $idContainer);

return $this->containers->updateContainer($idSite, $idContainer, $name, $description);
return $this->containers->updateContainer($idSite, $idContainer, $name, $description, $ignoreGtmDataLayer);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions Context/WebContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ public function generate($container)
$js = $this->addPreviewCode($baseJs, $hasPreviewRelease, $isPreviewRelease, $container);
$js = str_replace(array('/*!! initContainerHook */', '/*!!! initContainerHook */'), $initContainer, $js);

$ignoreGtmDataLayer = isset($container['ignoreGtmDataLayer']) && $container['ignoreGtmDataLayer'] == 1 ? 'true' : 'false';
$windowLevelSettingsJs = "var ignoreGtmDataLayer = {$ignoreGtmDataLayer};";
$js = str_replace(array('/*!! windowLevelSettingsHook */', '/*!!! windowLevelSettingsHook */'), $windowLevelSettingsJs, $js);

$path = $this->getJsTargetPath($container['idsite'], $container['idcontainer'], $release['environment'], $container['created_date']);
$filesCreated[$path] = $js;
$this->storage->save(PIWIK_DOCUMENT_ROOT . $path, $js);
Expand Down
4 changes: 3 additions & 1 deletion Dao/ContainersDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function install()
`context` VARCHAR(10) NOT NULL,
`name` VARCHAR(" . Name::MAX_LENGTH . ") NOT NULL,
`description` VARCHAR(" . Description::MAX_LENGTH . ") NOT NULL DEFAULT '',
`ignoreGtmDataLayer` TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
`status` VARCHAR(10) NOT NULL,
`created_date` DATETIME NOT NULL,
`updated_date` DATETIME NOT NULL,
Expand Down Expand Up @@ -68,7 +69,7 @@ public function hasContainer($idContainer)
return !empty($container);
}

public function createContainer($idSite, $idContainer, $context, $name, $description, $createdDate)
public function createContainer($idSite, $idContainer, $context, $name, $description, $createdDate, $ignoreGtmDataLayer)
{
if ($this->isContainerInUse($idContainer)) {
throw new Exception(Piwik::translate('TagManager_ErrorContainerIdDuplicate'));
Expand All @@ -85,6 +86,7 @@ public function createContainer($idSite, $idContainer, $context, $name, $descrip
'context' => $context,
'name' => $name,
'description' => !empty($description) ? $description : '',
'ignoreGtmDataLayer' => !empty($ignoreGtmDataLayer) ? $ignoreGtmDataLayer : 0,
'status' => $status,
'created_date' => $createdDate,
'updated_date' => $createdDate
Expand Down
19 changes: 12 additions & 7 deletions Model/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Piwik\Plugins\TagManager\Input\IdSite;
use Piwik\Plugins\TagManager\Input\Name;
use Piwik\Plugins\TagManager\Model\Container\ContainerIdGenerator;
use Piwik\Validators\NumberRange;


class Container extends BaseModel
Expand Down Expand Up @@ -143,7 +144,7 @@ public function getContainerInstallInstructions($idSite, $idContainer, $environm
}
}

private function validateContainer($idSite, $name, $description)
private function validateContainer($idSite, $name, $description, $ignoreGtmDataLayer)
{
$site = new IdSite($idSite);
$site->check();
Expand All @@ -153,18 +154,21 @@ private function validateContainer($idSite, $name, $description)

$description = new Description($description);
$description->check();

$numberRange = new NumberRange(0, 1);
$numberRange->validate($ignoreGtmDataLayer);
}

public function addContainer($idSite, $context, $name, $description)
public function addContainer($idSite, $context, $name, $description, $ignoreGtmDataLayer)
{
$this->validateContainer($idSite, $name, $description);
$this->validateContainer($idSite, $name, $description, $ignoreGtmDataLayer);
$this->contextProvider->checkIsValidContext($context);

$createdDate = $this->getCurrentDateTime();

$idContainer = $this->containerIdGenerator->generateId();

$this->dao->createContainer($idSite, $idContainer, $context, $name, $description, $createdDate);
$this->dao->createContainer($idSite, $idContainer, $context, $name, $description, $createdDate, $ignoreGtmDataLayer);

$this->versionsDao->createDraftVersion($idSite, $idContainer, $createdDate);

Expand All @@ -173,13 +177,14 @@ public function addContainer($idSite, $context, $name, $description)
return $idContainer;
}

public function updateContainer($idSite, $idContainer, $name, $description)
public function updateContainer($idSite, $idContainer, $name, $description, $ignoreGtmDataLayer)
{
$this->validateContainer($idSite, $name, $description);
$this->validateContainer($idSite, $name, $description, $ignoreGtmDataLayer);

$columns = array(
'name' => $name,
'description' => $description
'description' => $description,
'ignoreGtmDataLayer' => $ignoreGtmDataLayer
);
$this->updateContainerColumns($idSite, $idContainer, $columns);
$this->generateContainer($idSite, $idContainer);
Expand Down
2 changes: 2 additions & 0 deletions TagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,8 @@ public function getClientSideTranslationKeys(&$result)
$result[] = 'TagManager_SiteWithoutDataMtmIntro';
$result[] = 'TagManager_SiteWithoutDataMtmStep2';
$result[] = 'TagManager_SiteWithoutDataMtmStep3';
$result[] = 'TagManager_IgnoreGtmDataLaterDescription';
$result[] = 'TagManager_IgnoreGtmDataLaterTitle';
}

public function getStylesheetFiles(&$stylesheets)
Expand Down
64 changes: 64 additions & 0 deletions Updates/5.0.0-rc8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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\Plugins\TagManager\Template\Tag\MatomoTag;
use Piwik\Plugins\TagManager\UpdateHelper\NewTagParameterMigrator;
use Piwik\Updater;
use Piwik\Updater\Migration;
use Piwik\Updater\Migration\Factory as MigrationFactory;
use Piwik\Updates as PiwikUpdates;

/**
* Update for version 5.0.0-rc8.
*/
class Updates_5_0_0_rc8 extends PiwikUpdates
{
/**
* @var MigrationFactory
*/
private $migration;

public function __construct(MigrationFactory $factory)
{
$this->migration = $factory;
}

/**
* Return database migrations to be executed in this update.
*
* Database migrations should be defined here, instead of in `doUpdate()`, since this method is used
* in the `core:update` command when displaying the queries an update will run. If you execute
* migrations directly in `doUpdate()`, they won't be displayed to the user. Migrations will be executed in the
* order as positioned in the returned array.
*
* @param Updater $updater
* @return Migration\Db[]
*/
public function getMigrations(Updater $updater)
{
return array(
$this->migration->db->addColumn('tagmanager_container', 'ignoreGtmDataLayer', 'TINYINT(1) UNSIGNED NOT NULL DEFAULT 0', 'description'),
);
}

/**
* Perform the incremental version update.
*
* This method should perform all updating logic. If you define queries in the `getMigrations()` method,
* you must call {@link Updater::executeMigrations()} here.
*
* @param Updater $updater
*/
public function doUpdate(Updater $updater)
{
$updater->executeMigrations(__FILE__, $this->getMigrations($updater));
}
}
4 changes: 3 additions & 1 deletion javascripts/tagmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
var documentAlias = document;
var windowAlias = window;

/*!! windowLevelSettingsHook */

/*!! previewModeHook */

if (typeof window.MatomoTagManager !== 'object') {
Expand Down Expand Up @@ -1817,7 +1819,7 @@

dataLayer.push({'mtm.mtmScriptLoadedTime': timeScriptLoaded});

if ('undefined' !== typeof windowAlias.dataLayer && utils.isArray(windowAlias.dataLayer)) {
if (('undefined' === typeof ignoreGtmDataLayer || !ignoreGtmDataLayer) && 'undefined' !== typeof windowAlias.dataLayer && utils.isArray(windowAlias.dataLayer)) {
// compatibility for GTM
for ( i = 0; i < windowAlias.dataLayer.length; i++) {
if (utils.isObject(windowAlias.dataLayer[i])) {
Expand Down
Loading

0 comments on commit 7a28e88

Please sign in to comment.