I need to access customer information in JS, I have a analytics js file, I need to get some customer information like email, name etc. How do I access this in a JS file in magento.

If we are transfer the customer data from php to JS, where in the file structure will I put this code

2 Answers 2


Assumption: You'd like to track visitors as they are logged in and interacting with your site. You'd like to take unique customer attributes about the loggedIn Customer and pass them off to a javascript.

Getting Attributes of a LoggedIn Customer: A quick Google search yielded a plethora of getCustomerX methods, from http://www.kathirvel.com/magento-get-loggedin-customer-fullname-firstname-lastname-email-address/ (copying the below from this source):

// Check if any customer is logged in or not
if (Mage::getSingleton('customer/session')->isLoggedIn()) {

// Load the customer's data
$customer = Mage::getSingleton('customer/session')->getCustomer();

   $customer->getName(); // Full Name
   $customer->getFirstname(); // First Name
   $customer->getMiddlename(); // Middle Name
   $customer->getLastname(); // Last Name

// All other customer data
   $customer->getWebsiteId(); // ID
   $customer->getEntityId(); // ID
   $customer->getEntityTypeId(); // ID
   $customer->getAttributeSetId(); // ID
   $customer->getGroupId(); // ID
   $customer->getStoreId(); // ID
   $customer->getCreatedAt(); // yyyy-mm-ddThh:mm:ss+01:00
   $customer->getUpdatedAt(); // yyyy-mm-dd hh:mm:ss
   $customer->getIsActive(); // 1
   $customer->getCreatedIn(); // Admin
   $customer->getGender(); // ID
   $customer->getDefaultBilling(); // ID
   $customer->getDefaultShipping(); // ID
   $customer->getDob(); // yyyy-mm-dd hh:mm:ss
   $customer->getTaxClassId(); // ID

Passing the PHP to your Javascript: In order to pass these to Javascript, you'd need to turn them into variables by echoing the data in your PHP variables into the . Here's an example of a few:

   var customerFullName = '<?php echo $customer->getName(); ?>';
   var customerEmail = '<?php echo $customer->getEmail(); ?>';

Additionally, see this helpful answer on getting magic methods with Magento: https://stackoverflow.com/questions/5517491/how-do-i-learn-the-methods-that-can-be-accessed-for-certain-objects, which should help you how to determine which attributes to call and how (as well as custom attributes).

Analytics: As far as your analytics script, it'd be difficult for anyone to tell you how to pass the JS variables off to it without knowing which analytics tool you're using (Google or otherwise).

Placing the Code: If you're using Google Analytics, then you'd probably want to place the code within the ga.phtml file, located at app/design/frontend/base/default/template/googleanalytics/ga.phtml. It's probably better not to modify your default theme, so take ga.phtml and place it in the app/design/frontend/yourtheme/default/template/googleanalytics instead (you may have to create the folders)

  • Thanks for the answer. We are using lot of different tracking tags on the site, some of them need customer and order information for tracking. I have created a analytics.phtml file in directory app/design/frontend/base/default/template/analytics and loaded the phtml file in page.xml using the block type xml declaration Sep 28, 2015 at 19:31
  • That is perfectly good practice, then. Good luck. Sep 28, 2015 at 20:06

You would do most of the work outside of the javascript. Something like...

<?php $_customer = $this->helper('customer')->getCurrentCustomer(); ?>
<?php if ($_customer->getId()): ?>
        var email = '<?php echo $this->escapeHtml($_customer->getEmail())';
         // do more js things here
<?php endif; ?>

This could be tidied up by moving some of it in to a custom block.

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.