Skip to content

Commit

Permalink
Added code to support pre configured variables, #PG-3109
Browse files Browse the repository at this point in the history
  • Loading branch information
AltamashShaikh committed Nov 27, 2023
1 parent bb04c30 commit 9b5f70d
Show file tree
Hide file tree
Showing 35 changed files with 120 additions and 59 deletions.
17 changes: 17 additions & 0 deletions Context/BaseContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ protected function generatePublicContainer($container, $release)
return $containerJs;
}

public function getPreConfiguredVariablesJSCodeResponse($context)
{
$response = ['keys' => [], 'values' => []];
$preConfiguredVariables = $this->variablesProvider->getPreConfiguredVariables();
foreach ($preConfiguredVariables as $variable) {
if (method_exists($variable, 'getDataLayerVariableJs')) {
$response['keys'][] = '{{' . $variable->getId() . '}}';
$response['values'][] = $variable->getDataLayerVariableJs();
} else if (method_exists($variable, 'loadTemplate')) {
$response['keys'][] = '{{' . $variable->getId() . '}}';
$response['values'][] = '(function(){' . $variable->loadTemplate($context, $variable, true) . '})()';
}
}

return $response;
}

private function parametersToVariableJs($container, $entity)
{
if (!empty($entity['name'])) {
Expand Down
12 changes: 12 additions & 0 deletions Context/BaseContext/TemplateLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,16 @@ public function loadVariableTemplate($variable, $contextId)
}
}

public function updateVariableTemplate($methodName, $template)
{
$this->templateFunctions[$methodName] = $template;
}

public function getVariableTemplate($methodName)
{
if ($this->templateFunctions[$methodName]) {
return $this->templateFunctions[$methodName];
}
}

}
5 changes: 5 additions & 0 deletions Context/WebContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function generate($container)
}

$baseJs = $this->javaScriptTagManagerLoader->getJavaScriptContent();
$preconfiguredVariablesResponse = $this->getPreConfiguredVariablesJSCodeResponse(self::ID);

foreach ($environments as $environment) {
$environmentId = $environment['id'];
Expand Down Expand Up @@ -144,6 +145,10 @@ public function generate($container)
foreach ($containerJs['variables'] as &$variable) {
$variable['Variable'] = $this->templateLocator->loadVariableTemplate($variable, self::ID);
$variable['parameters'] = $this->addVariableTemplateToParameters($variable['parameters']);
if (!empty($variable['parameters']['jsFunction']) && strpos($variable['parameters']['jsFunction'], '{{')!==FALSE) {
$variable['parameters']['jsFunction'] = str_replace($preconfiguredVariablesResponse['keys'], $preconfiguredVariablesResponse['values'], $variable['parameters']['jsFunction']);
$this->templateLocator->updateVariableTemplate($variable['Variable'], str_replace($preconfiguredVariablesResponse['keys'], $preconfiguredVariablesResponse['values'], $this->templateLocator->getVariableTemplate($variable['Variable'])));
}

if (!$isPreviewRelease) {
$variable['name'] = $variable['type'];
Expand Down
8 changes: 8 additions & 0 deletions Template/Variable/PreConfigured/BaseDataLayerVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public function loadTemplate($context, $entity)
}
}

public function getDataLayerVariableJs()
{
$dataLayerVariableName = $this->getDataLayerVariableName();
if ($dataLayerVariableName) {
return "TagManager.dataLayer.get('$dataLayerVariableName')";
}
}

protected function makeDataLayerTemplateMethod($dataLayerKey)
{
$js = 'return parameters.container.dataLayer.get("' . $dataLayerKey . '")';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final public function getParameters()
return [];
}

protected function makeReturnTemplateMethod($js)
protected function makeReturnTemplateMethod($js, $skipTemplate = false)
{
$js = trim($js);
if (!Common::stringEndsWith($js, ';')) {
Expand All @@ -35,6 +35,10 @@ protected function makeReturnTemplateMethod($js)
$js = 'return ' . $js;
}

if ($skipTemplate) {
return $js;
}

return '(function () { return function (parameters, TagManager) { this.get = function () { ' . $js . ' }; } })();';
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/BrowserLanguageVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_DEVICE;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('navigator.language');
return $this->makeReturnTemplateMethod('navigator.language', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/ContainerIdVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_CONTAINER_INFO;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('parameters.container.id');
return $this->makeReturnTemplateMethod('parameters.container.id', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/ContainerRevisionVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_CONTAINER_INFO;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('parameters.container.revision');
return $this->makeReturnTemplateMethod('parameters.container.revision', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/ContainerVersionVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_CONTAINER_INFO;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('parameters.container.versionName');
return $this->makeReturnTemplateMethod('parameters.container.versionName', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/DnsLookupTimeVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public function getCategory()
return self::CATEGORY_PERFORMANCE;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod("TagManager.window.getPerformanceTiming('domainLookupEnd') - TagManager.window.getPerformanceTiming('domainLookupStart')");
return $this->makeReturnTemplateMethod("TagManager.window.getPerformanceTiming('domainLookupEnd') - TagManager.window.getPerformanceTiming('domainLookupStart')", $skipTemplate);
}
}
}
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/EnvironmentVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_CONTAINER_INFO;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('parameters.container.environment');
return $this->makeReturnTemplateMethod('parameters.container.environment', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/IsoDateVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_DATE;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('new Date().toISOString()');
return $this->makeReturnTemplateMethod('new Date().toISOString()', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/LocalDateVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_DATE;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('new Date().toDateString()');
return $this->makeReturnTemplateMethod('new Date().toDateString()', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/LocalHourVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_DATE;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('new Date().getHours()');
return $this->makeReturnTemplateMethod('new Date().getHours()', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/LocalTimeVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_DATE;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('new Date().toTimeString()');
return $this->makeReturnTemplateMethod('new Date().toTimeString()', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/PageHashVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_PAGE_VARIABLES;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('String(parameters.window.location.hash).replace("#", "")');
return $this->makeReturnTemplateMethod('String(parameters.window.location.hash).replace("#", "")', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/PageHostnameVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_PAGE_VARIABLES;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('parameters.window.location.hostname');
return $this->makeReturnTemplateMethod('parameters.window.location.hostname', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/PageLoadTimeTotal.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public function getCategory()
return self::CATEGORY_PERFORMANCE;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod("TagManager.window.getPerformanceTiming('loadEventEnd') - TagManager.window.getPerformanceTiming('navigationStart')");
return $this->makeReturnTemplateMethod("TagManager.window.getPerformanceTiming('loadEventEnd') - TagManager.window.getPerformanceTiming('navigationStart')", $skipTemplate);
}
}
}
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/PageOriginVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_PAGE_VARIABLES;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('parameters.window.location.origin');
return $this->makeReturnTemplateMethod('parameters.window.location.origin', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/PagePathVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_PAGE_VARIABLES;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('parameters.window.location.pathname');
return $this->makeReturnTemplateMethod('parameters.window.location.pathname', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/PageRenderTimeVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public function getCategory()
return self::CATEGORY_PERFORMANCE;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod("TagManager.window.getPerformanceTiming('domComplete') - TagManager.window.getPerformanceTiming('domLoading')");
return $this->makeReturnTemplateMethod("TagManager.window.getPerformanceTiming('domComplete') - TagManager.window.getPerformanceTiming('domLoading')", $skipTemplate);
}
}
}
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/PageTitleVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_PAGE_VARIABLES;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('parameters.document.title');
return $this->makeReturnTemplateMethod('parameters.document.title', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/PageUrlVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_PAGE_VARIABLES;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('parameters.window.location.href');
return $this->makeReturnTemplateMethod('parameters.window.location.href', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/PreviewModeVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_CONTAINER_INFO;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('parameters.container.environment === "preview" ? "1" : "0"');
return $this->makeReturnTemplateMethod('parameters.container.environment === "preview" ? "1" : "0"', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/RandomNumberVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_UTILITIES;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('Math.floor(Math.random() * 20000000000)');
return $this->makeReturnTemplateMethod('Math.floor(Math.random() * 20000000000)', $skipTemplate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Template/Variable/PreConfigured/ReferrerVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function getCategory()
return self::CATEGORY_PAGE_VARIABLES;
}

public function loadTemplate($context, $entity)
public function loadTemplate($context, $entity, $skipTemplate = false)
{
switch ($context) {
case WebContext::ID:
return $this->makeReturnTemplateMethod('parameters.document.referrer');
return $this->makeReturnTemplateMethod('parameters.document.referrer', $skipTemplate);
}
}

Expand Down
Loading

0 comments on commit 9b5f70d

Please sign in to comment.