Skip to content

Commit

Permalink
Merge pull request #3318 from romainruaud/fix_tracker-js-minifying-210x
Browse files Browse the repository at this point in the history
[Tracker] Fix #3301, improving tracker compatibility with template mi…
  • Loading branch information
romainruaud authored Jul 10, 2024
2 parents 0cfaadb + 0c29c67 commit 9c0ac8b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,39 @@
<?php
$jsonHelper = $block->getJsonHelper();
if ($block->isEnabled()) {
$beaconUrl = $this->escapeJsQuote($block->getBeaconUrl());
$telemetryUrl = $this->escapeJsQuote($block->getTelemetryUrl());
$telemetryEnabled = $this->escapeJsQuote($block->isTelemetryEnabled());
$sessionConfig = $jsonHelper->jsonEncode($block->getCookieConfig());
$trackerApiConfig = "";
$beaconUrl = $this->escapeJsQuote($block->getBeaconUrl());
$telemetryUrl = $this->escapeJsQuote($block->getTelemetryUrl());
$telemetryEnabled = $this->escapeJsQuote($block->isTelemetryEnabled());
$sessionConfig = $jsonHelper->jsonEncode($block->getCookieConfig());
$trackerApiConfig = "";

if ($block->isUsingAPI()) {
$endpointUrl = $this->escapeJsQuote($block->getEndpointUrl());
$trackerApiConfig .= "endpointUrl : '{$endpointUrl}'";
}

$scriptString = "
//<![CDATA[
try {
smileTracker.setConfig({
beaconUrl: '{$beaconUrl}',
telemetryUrl: '{$telemetryUrl}',
telemetryEnabled: '{$telemetryEnabled}',
sessionConfig: {$sessionConfig},
{$trackerApiConfig}
});
";
if ($block->isUsingAPI()) {
$endpointUrl = $this->escapeJsQuote($block->getEndpointUrl());
$trackerApiConfig .= "endpointUrl:'{$endpointUrl}',";
}

$storeId = $this->escapeJsQuote($block->getStoreId());
$userConsentScript = $this->escapeJsQuote($block->getUserConsentScript());
$userConsentConfig = $jsonHelper->jsonEncode($block->getUserConsentConfig());
$storeId = $this->escapeJsQuote($block->getStoreId());
$userConsentScript = $this->escapeJsQuote($block->getUserConsentScript());
$userConsentConfig = $jsonHelper->jsonEncode($block->getUserConsentConfig());

$scriptString .= "
smileTracker.addPageVar('store_id', '{$storeId}');
require(['{$userConsentScript}'], function (userConsent) {
if (userConsent({$userConsentConfig})) {
smileTracker.sendTag();
}
});
} catch (err) {
;
}
//]]>
";
$scriptString = "//<![CDATA[\n";
$scriptString .= "try{";
$scriptString .= "smileTracker.setConfig({";
$scriptString .= "beaconUrl:'{$beaconUrl}',";
$scriptString .= "telemetryUrl:'{$telemetryUrl}',";
$scriptString .= "telemetryEnabled:'{$telemetryEnabled}',";
$scriptString .= "sessionConfig:{$sessionConfig},";
$scriptString .= "{$trackerApiConfig}";
$scriptString .= "});";
$scriptString .= "smileTracker.addPageVar('store_id','{$storeId}');";
$scriptString .= "require(['{$userConsentScript}'],function(userConsent){";
$scriptString .= "if(userConsent({$userConsentConfig})){";
$scriptString .= "smileTracker.sendTag();";
$scriptString .= "}";
$scriptString .= "});";
$scriptString .= "}catch(err){;}\n";
$scriptString .= "//]]>\n";

echo /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false);
echo /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false);
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,21 @@
/**
* @var $block Smile\ElasticsuiteTracker\Block\Variables\AbstractBlock
* @var $secureRenderer Magento\Framework\View\Helper\SecureHtmlRenderer
*/ ?>
*/
?>
<?php
$variables = $block->getVariables();
if (!empty($variables)) {
$scriptString = "
//<![CDATA[
try {
";
$scriptString = "try {\n";

foreach ($variables as $varName => $value) {
$escapedVarName = $this->escapeJsQuote($varName);
$escapedValue = $block->stripTags($this->escapeJsQuote($value), null, true);
$scriptString .= "smileTracker.addPageVar('{$escapedVarName}', '{$escapedValue}');\n";
}
foreach ($variables as $varName => $value) {
$escapedVarName = $this->escapeJsQuote($varName);
$escapedValue = $block->stripTags($this->escapeJsQuote($value), null, true);
$scriptString .= "smileTracker.addPageVar('{$escapedVarName}', '{$escapedValue}');\n";
}

$scriptString .= "
} catch (err) {
;
}
//]]>
";
$scriptString .= "} catch (err) { ; }";

echo /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false);
echo /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false);
}
?>

0 comments on commit 9c0ac8b

Please sign in to comment.