How do I add javascript tracking code to all magento pages? - magento

I need to add java script tracking code to all pages of my magento store just before the closing "body" tag. Does anyone know what file I need to put this in? And do I have to modify the JavaScript at all for Magento or just paste it in?

To answer your question: How do I add javascript to every page
Within the template, typically you load a footer block on every page, so go and find the phtml file here;
app/design/frontend/XXX/YYY/template/page/html/footer.phtml
Where xxx/yyy is your theme directory. Add the tracking code here and it will load on every page.
Regarding whether it needs modification - that really depends what you expect to do. If you need to insert variables such as basket values, customer number (if present) etc, then yes you will need to add those php variables to it, and here is where you could run into trouble. If you are using caching, then the footer may not be the best place as you could end up with cached variable values in the snippet. If it dynamically pulls it values for things such as page url, then you'll be ok.
The other problem is that this will load in your checkout pages - so if it calls a js file from an http:// location, your page will fail SSL validation.

You can do this by adding a layout file to your module configuration:
<config>
...
<frontend>
<layout>
<updates>
<mymodule>
<file>mymodule.xml</file>
</mymodule>
</updates>
</layout>
</frontend>
...
</config>
Add the mymodule.xml file into the app/design/frontend/base/default/layout/ folder and put the following content in there:
<layout version="1.0.0">
<default>
<reference name="before_body_end">
<block type="core/template" name="mytracking.block" after="-" template="mynamespace/mymodule/tracking.phtml">
<action method="setCacheLifetime"><s>0</s></action>
</block>
</reference>
</default>
</layout>
Now you can add your tracking javascript code in this template file app/design/frontend/base/default/template/mynamespace/mymodule/tracking.phtml. It will be printed in all Magento pages.
EDIT
I've added a piece of code that disables the cache for your tracking block, thanks to #PixieMedia for letting me know.

Easiest way - via the backend, there is a config section specifically for this need.
System -> Configuration -> General -> Design Section -> Footer Tab -> Miscellaneous HTML
It relies on your theme calling the getAbsoluteFooter() method in your main template files right before the closing body tag, just be aware of that.
<?php echo $this->getAbsoluteFooter() ?>
</body>

Related

Can we disable magento extension for particular page

how to disable a extension for particular page for example i want to disable extension for homepage only in magento
<config>
<modules>
<first_module>
<active>false</active>
<codepool>local</codepool>
</first_module>
</modules>
</config>
I haven't confronted this kinda disabling feature that way. What we usually do is, we remove the module on the page from local.xml under the page responsible layout handle taking responsible reference.
For eg. if i don't one particular module for eg.newsletter on homepage then,
<cms_index_index> <!--/ Homepage -->
<reference name="footer"> <!--/ Reference where the module appearing -->
<remove name="newsletter"/> <!--/ Remove -->
</reference>
</cms_index_index>
That is not possible in Magento 1.x
You can only enable/disable a module per store view or website.

Magento 1.9 - modifying layout

What is the best way of modifying a layout for Magento 1.9? I've been updating layouts using the admin backend but now need to do this programatically. Several articles have mentioned using layout.xml but for some reason, my system isn't picking up the layout updates.
Specifically, this is what I'm trying to modify:
Original layout
<block type="catalog/product_view_options" name="product.info.options" as="product_options" template="catalog/product/view/options.phtml">
<action method="addOptionRenderer">
<type>text</type>
<block>catalog/product_view_options_type_text</block>
<template>catalog/product/view/options/type/text.phtml</template>
</action>
...
</block>
Modified layout
<block type="catalog/product_view_options" name="product.info.options" as="product_options" template="catalog/product/view/options.phtml">
<action method="addOptionRenderer">
<type>text</type>
<block>catalog/product_view_options_type_text</block>
<template>catalog/product/view/options/type/newfile.phtml</template>
</action>
...
</block>
My modified layout is located in:app/design/frontend/default/(company name)/(module name)/layout/layout.xml
layout.xml (just checking to see if it will add the block, "test"):
<layout>
<default>
<block type="core/text_list" name="test"/>
</default>
</layout>
app/code/local/(company name)/(module name)/etc/config.xml:
<config>
<modules>
...
</modules>
<frontend>
<layout>
<updates>
<(module name)>
<file>local.xml</file>
</(module name)>
</updates>
</layout>
</frontend>
</config>
Can somebody point me in the right direction? Thanks!
Here are two things I notice which should be changed:
Your layout update file should have a unique name that won’t conflict with existing modules. In your config.xml you have specified <file>local.xml</file>. The problem with this is, local.xml will already be loaded automatically by Magento. So you should not use that filename for your layout update file if you are trying to add a new one specific to your module, however you may add miscellaneous updates to that file and they will get picked up automatically and applied last to the final merged layout.
Behind the scenes there’s an <updates /> section of the global config that contain nodes with all the file names to load. Once the files listed in the config have been combined, Magento will merge in one last xml file, local.xml.
Your layout update file app/design/frontend/default/(company name)/(module name)/layout/layout.xml does not appear to be in the right location. You should create your new layout update file at: app/design/frontend/base/default/layout/(company name)/(module_name).xml. Then, you need to update your config.xml to point to this file: <file>(company name)/(module name).xml</file>.
Because of the ability to use different themes in Magento and its fallback mechanism, all extension related theme files are placed in “base/default” package for frontend...

How do you add a custom layout to Magento that survives an upgrade?

I added the following to /app/etc/local.xml, which works. Obviously I need these changes to survive an upgrade. Did I do this correctly?
<global>
...
<page>
<layouts>
<home module="page" translate="label">
<label>Home</label>
<template>page/home.phtml</template>
<layout_handle>page_home</layout_handle>
</home>
<product module="page" translate="label">
<label>Product</label>
<template>page/product.phtml</template>
<layout_handle>page_product</layout_handle>
</product>
</layouts>
</page>
</global>
No, you did not do this correctly. The app/etc/local.xml file should contain only the configuration items Magento needs to bootstrap it's environment.
The "correct" (i.e. following community standards, and therefore giving you the best chance at support in the future) is to create your own Magento module and add the custom configuration there. Beyond being the right way to do things, this also makes it easier to distribute these changes to other Magento users.

Magento Google Analytics Content Experiments

What is the simplest way to implement Google Analytics Content Experiment into Magento CE?
I want to test home page content.
Adding the Testing Code to CMS pages does not work. Testing Tag is after Analytics Tag.
Does Magento support adding the Testing Tag via admin Interface?
Or is there a simple way to add the tag via XML Layout Update?
Would it be possible to use Google Tag Manager to add the codes we need?
Tags could be installed after body like:
Add to local.xml in your layout folder following code:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Last loaded layout file
-->
<layout>
<default>
<reference name="after_body_start">
<block type="cms/block" name="tags-first-in-body" before="-">
<action method="setBlockId"><block_id>tags-first-in-body</block_id></action>
</block>
</reference>
</default>
</layout>
and add a static block called tags-first-in-body.
Insert code to this block.

Magento: how do I over-ride an admin js file instead of editing it in core?

Recently answered this question with a change to a core file:
Approach on changing row color on orders grid in admin
There is one file that needed updating to answer this question: js/mage/adminhtml/grid.js
What I would like to know is how to do this properly, i.e. without editing the core file.
Edit
This is what worked:
in app/design/adminhtml/default/default/layout/local.xml:
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="head">
<action method="removeItem"><type>js</type><name>mage/adminhtml/grid.js</name></action>
<action method="addItem"><type>js</type><name>colors/adminhtml/grid.js</name></action>
</reference>
</default>
</layout>
Then I put a new directory + subdirectory and file in js/colors/adminhtml/grid.js
And many thanks to Alan for giving me the 'courage' to do it properly...
Add a Javascript Block to the Page, using events, the adminhtml application's local.xml, or a custom module layout xml file
Add a Prototype dom:loaded event listener to the page
Using Prototype's $$ method, search the dom for the order status HTML DOM nodes. If possible, use an ID in there to ensure you're getting the right nodes.
Use Prototype's addClassName method to add a css class to the nodes from #3.
Add a CSS file to the page, using events, the adminhtml application's local.xml, or a custom module layout xml file. In this CSS file add style definitions to get the colors you want.

Resources