I have added java-script in the footer like below and I am passing the customer email dynamically, but the same email id is displaying for all the customers because the page is caching.

enter image description here

Please help me to fix this cache issue.

1 Answer 1


First of all, avoiding using Object Manager directly in our template. This is not good for best practice. Should create a new block and try to apply the solutions below:

Solutions for Caching issue

Using after.body.start or before_body_end

[Seem that these blocks are cached] These blocks are not cached. So, we can add your custom block to these containers. For example:


<?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">
    <referenceContainer name="before.body.end">
        <block class='Company\Module\Block\Module' name="js.tracking" as="js.tracking" template="Company_Module::tracking.phtml"/>
     <!--<referenceContainer name="after.body.start">-->
        <!--<block class='Company\Module\Block\Module' name="js.tracking" as="js.tracking" template="Company_Module::tracking.phtml"/>-->

Add tracking js to your custom template: Company/Module/view/frontend/templates/tracking.phtml

Should take a look at Google Analytics module: vendor/magento/module-google-analytics.

Disable caching of the block

Read more here: How do disable caching of custom block on product view page?. However, currently, this way should not try because make the pages contain this block uncacheable.


I like this way, we should try with Ajax.

--Suggests other solutions:

Local storage

Magento 2 stores cart info on local storage, we can add more customer info to cart info. And then, use it in our custom script.

Global Js variable

Go to vendor/magento/module-checkout/view/frontend/templates/onepage.phtml, we can see that Magento uses some global variables to store checkout info(including customer data).

  • 2
    +1 for mentioning that cacheable="false" prevents the whole page from being cached, not enough people know about this.
    – Ben Crook
    Mar 23, 2017 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.