Documentation menuEcommerce: Magento

Ecommerce

Add Loogika to Magento

You add Loogika to Magento in the design configuration of the Admin, without editing a theme. The Scripts and Style Sheets field prints the tag into the head of every storefront page. From version 2.4.7 the checkout page also needs a Content Security Policy entry for Loogika.

Last updated .

What do you need before you start?

Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. It looks like this, with your own 12-character site ID in place of YOUR_SITE_ID:

<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>

The steps on this page apply to both Magento Open Source and Adobe Commerce.

How do you add the Loogika tag in the Magento Admin?

Paste the tag into the HTML Head section of the design configuration, then refresh the cache.

  1. In the Admin sidebar, go to Content, then Design, then Configuration.
  2. Each row stands for a scope. Find the scope that you want to track, which is usually the store view. Click Edit in the Action column.
  3. Expand HTML Head.
  4. Go to the bottom of the Scripts and Style Sheets field and paste the tag there. Click Save Configuration.
  5. Go to System, then Cache Management, and refresh the cache types that are marked as invalidated.

Purge Varnish or your CDN as well if you use one. Visitors otherwise keep getting stored pages without the tag.

Why does the Magento checkout need a Content Security Policy entry?

Magento 2.4.7 and later block scripts from unlisted hosts on the checkout page. A Content Security Policy (CSP) is a list of the sources a page may load code from. Since 2.4.7 the checkout page, checkout_index_index, runs the policy in restrict mode. The rest of the storefront remains in report-only mode.

A csp_whitelist.xml file in a module of your own allows Loogika in both script-src and connect-src. Like any module, it also needs a registration.php file and an etc/module.xml file. Save the file below as app/code/Vendor/Loogika/etc/csp_whitelist.xml:

<?xml version="1.0"?>
<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp:etc/csp_whitelist.xsd">
    <policies>
        <policy id="script-src">
            <values>
                <value id="loogika" type="host">https://app.loogika.com</value>
            </values>
        </policy>
        <policy id="connect-src">
            <values>
                <value id="loogika" type="host">https://app.loogika.com</value>
            </values>
        </policy>
    </policies>
</csp_whitelist>

After you add the module, run bin/magento setup:upgrade and then flush the cache.

How do you track purchases in Magento?

Add a block to the order success page that reads the order and sends a purchase event. The success page has a container named order.success.additional.info for extra content. The three files below go into the same module as the CSP file.

The block class reads the order from the checkout session. Save it as app/code/Vendor/Loogika/Block/Purchase.php:

<?php
namespace Vendor\Loogika\Block;

class Purchase extends \Magento\Checkout\Block\Onepage\Success
{
    public function getOrder(): \Magento\Sales\Model\Order
    {
        return $this->_checkoutSession->getLastRealOrder();
    }
}

The layout file places the block in the container. Save it as app/code/Vendor/Loogika/view/frontend/layout/checkout_onepage_success.xml:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="order.success.additional.info">
            <block class="Vendor\Loogika\Block\Purchase" name="loogika.purchase" template="Vendor_Loogika::purchase.phtml" cacheable="false"/>
        </referenceContainer>
    </body>
</page>

The template prints the script that sends the event. Save it as app/code/Vendor/Loogika/view/frontend/templates/purchase.phtml:

<?php
/** @var \Vendor\Loogika\Block\Purchase $block */
$order = $block->getOrder();
if (!$order->getId()) {
    return;
}
$purchase = [
    'transaction_id' => $order->getIncrementId(),
    'value'          => (float) $order->getGrandTotal(),
    'currency'       => $order->getOrderCurrencyCode(),
];
?>
<script>
document.addEventListener('DOMContentLoaded', function () {
    window.loogika?.event('purchase', <?= /* @noEscape */ json_encode($purchase) ?>);
});
</script>

The script waits for DOMContentLoaded because the Loogika tag loads with defer. The order object also offers getTaxAmount() and getShippingAmount() if you want tax and shipping as properties.

How do you check that Loogika is installed?

Load your storefront in a fresh browser tab and visit a few pages, including the checkout. The pageviews show up in the Loogika dashboard within a few seconds. If nothing arrives, open the page source and look for script.js?siteId=.

Why does Magento not send data to Loogika?

SymptomCause and fix
The page source does not contain the tagDesign configuration works per scope, and the row of a store view wins over the rows for website and global. Edit the row that belongs to the storefront you test. Then flush Varnish and the page cache
Every page is tracked except the checkoutThe restrict mode on the checkout page blocks the script. Make sure the module is enabled. Then check the browser console for Content Security Policy errors that name app.loogika.com
The purchase event is missingThe getLastRealOrder() call only returns the order in the session that placed it. The success page sends nothing when you open it again later