Add this to your one of your layout xml files.
<cms_index_index><!-- handle for homepage -->
<reference name="content">
<block type="core/template" template="affiliate/homepage.phtml" name="affiliate" as="affiliate" after="-" />
</reference>
</cms_index_index>
<catalog_product_view><!-- handle for product view page -->
<reference name="content">
<block type="core/template" template="affiliate/product.phtml" name="affiliate" as="affiliate" after="-" />
</reference>
</catalog_product_view>
<checkout_cart_index><!-- handle for cart page -->
<reference name="content">
<block type="core/template" template="affiliate/cart.phtml" name="affiliate" as="affiliate" after="-" />
</reference>
</checkout_cart_index>
then create the files affiliate/homepage.phtml
, affiliate/product.phtml
, affiliate/cart.phtml
inside the folder app/design/frontend/base/default/temaplte/
and add your corresponding codes in each one of the files.
If the codes are the same (but I doubt it) you can use the same template.
If you need the product id or sku inside the tracking code for the product page you can get it like this:
<?php productId = Mage::registry('current_product')->getId()?>
<?php productSku = Mage::registry('current_product')->getSku()?>