{"id":1850,"date":"2024-10-07T14:55:17","date_gmt":"2024-10-07T11:55:17","guid":{"rendered":"https:\/\/plumrocket.com\/learn\/?p=1850"},"modified":"2024-10-10T11:00:26","modified_gmt":"2024-10-10T08:00:26","slug":"how-to-add-affiliate-tracking-code-to-magento-2","status":"publish","type":"post","link":"https:\/\/plumrocket.com\/learn\/add-magento-affiliate-tracking-code","title":{"rendered":"How to Add Affiliate Tracking Code to Magento 2"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full disable_zoom\"><img loading=\"lazy\" width=\"1600\" height=\"600\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code.png\" alt=\"How to Add Affiliate Tracking Code to Magento 2\" class=\"wp-image-1897\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code.png 1600w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code-300x113.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code-1024x384.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code-768x288.png 768w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code-1536x576.png 1536w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code-1568x588.png 1568w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/figure>\n\n\n\n<p>Integrating <a href=\"\/magento-affiliate-programs\" target=\"_blank\" rel=\"noreferrer noopener\">affiliate programs<\/a> into an online store is an excellent opportunity to increase its audience and sales. There are plenty of affiliate programs available so that online retailers can always find beneficial conditions for collaboration.<\/p>\n\n\n\n<p>Among different types of affiliate programs, Pay Per Sale (PPS) and Pay Per Lead (PPL) are the most commonly used. In the PPS program, the tracking code is placed on the checkout success page to confirm that the purchase was successful, while the PPL program tracks account registrations.<\/p>\n\n\n\n<p>Affiliate programs rely on JS tags, also known as pixels, to track conversions. However, adding them to a Magento 2 website can cause some difficulties for people who are not tech-savvy. This tutorial provides a general understanding and a detailed guide on how to add a Magento 2 affiliate tracking code to the checkout page for the Pay Per Sale program.<\/p>\n\n\n\n<p>Let\u2019s discuss two options for adding Magento 2 affiliate tracking code:<\/p>\n\n\n\n<ul><li><a href=\"#how-to-add-magento-2-affiliate-tracking-code-using-custom-module\">Manually, by creating a custom module<\/a> (for developers)<\/li><li><a href=\"#how-to-add-affiliate-tracking-code-in-magento-2-using-magento-2-affiliate-programs-extension\">Using Affiliate Programs extension<\/a><\/li><\/ul>\n\n\n\n<h2 id=\"how-to-add-magento-2-affiliate-tracking-code-using-custom-module\">1. How to Add a Magento 2 Affiliate Tracking Code Using a Custom Module<\/h2>\n\n\n\n<p>To begin with, you need to create a custom Magento module. For this, you need to create a folder hierarchy from the root of the Magento <code>app\/code\/Vendor\/Module<\/code>, where you should replace Vendor and Module with your own values. <\/p>\n\n\n\n<p><strong>Please note: <\/strong>In the code snippets below, we changed <code>Vendor_Module<\/code> to <code>Developer_AffiliateTracking<\/code>, as an example. So, while using these snippets, don&#8217;t forget to substitute your own values.<\/p>\n\n\n\n<p>Next, in the module, you need to create the <code>registration.php<\/code><em> <\/em>file and the <code>etc\/module.xml<\/code> file with the following content:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">Create the registration.php file with the following content:<\/div><pre><code class=\"language-php\">&lt;?php\n\nuse Magento\\Framework\\Component\\ComponentRegistrar;\n\nComponentRegistrar::register(ComponentRegistrar::MODULE, 'Developer_AffiliateTracking', __DIR__);\n<\/code><\/pre><\/div>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">Create the etc\/module.xml file with the following content:<\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Developer_AffiliateTracking\" >\n        &lt;sequence>\n        &lt;\/sequence>\n    &lt;\/module>\n&lt;\/config>\n<\/code><\/pre><\/div>\n\n\n\n<p>Since the PPS program tracks the fact of product purchases, the affiliate tracking code for Magento must be added to the checkout success page. To do this, create a <code>view\/frontend\/layout\/checkout_onepage_success.xml<\/code> file within the module and add the following code:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" layout=\"1column\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;body>\n        &lt;referenceContainer name=\"content\">\n            &lt;block class=\"Developer\\AffiliateTracking\\Block\\TrackingCode\" name=\"affiliate.tracking.code\" template=\"Developer_AffiliateTracking::tracking_code.phtml\" before=\"-\"\/>\n        &lt;\/referenceContainer>\n    &lt;\/body>\n&lt;\/page>\n<\/code><\/pre><\/div>\n\n\n\n<p>Next, for the layout to work correctly, you need to create two additional files: <code>Block\/TrackingCode.php<\/code> and <code>view\/frontend\/templates\/tracking_code.phtml<\/code>, with the following code:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">Create a Block\/TrackingCode.php file with the following code:<\/div><pre><code class=\"language-php\">&lt;?php\n\ndeclare(strict_types=1);\n\nnamespace Developer\\AffiliateTracking\\Block;\n\nuse Magento\\Checkout\\Model\\Session;\nuse Magento\\Customer\\Model\\Session as CustomerSession;\nuse Magento\\Framework\\View\\Element\\Template;\n\nclass TrackingCode extends Template\n{\n\n    \/**\n     * @var \\Magento\\Checkout\\Model\\Session\n     *\/\n    private Session $checkoutSession;\n\n    \/**\n     * @var \\Magento\\Customer\\Model\\Session\n     *\/\n    private CustomerSession $customerSession;\n\n    \/**\n     * @param \\Magento\\Checkout\\Model\\Session                  $checkoutSession\n     * @param \\Magento\\Customer\\Model\\Session                  $customerSession\n     * @param \\Magento\\Framework\\View\\Element\\Template\\Context $context\n     * @param array                                            $data\n     *\/\n    public function __construct(\n        Session $checkoutSession,\n        CustomerSession $customerSession,\n        Template\\Context $context,\n        array $data = []\n    ) {\n        parent::__construct($context, $data);\n        $this->checkoutSession = $checkoutSession;\n        $this->customerSession = $customerSession;\n    }\n\n    \/**\n     * @return string\n     *\/\n    public function getPartnerId()\n    {\n        return '123456789';\n    }\n}\n<\/code><\/pre><\/div>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">Create a view\/frontend\/templates\/tracking_code.phtml file with the following code:<\/div><pre><code class=\"language-php\">&lt;?php\n\/** @var Developer\\AffiliateTracking\\Block\\TrackingCode $block *\/\n?>\n\n&lt;!-- Criteo Loader File -->\n&lt;script type=\"text\/javascript\"\n        src=\"\/\/dynamic.criteo.com\/js\/ld\/ld.js?a=&lt;?= $block->getPartnerId(); ?>\">\n&lt;\/script>\n&lt;!-- END Criteo Loader File -->\n\n&lt;!-- Criteo Sales Tag -->\n&lt;script type=\"text\/javascript\">\n    window.criteo_q = window.criteo_q || [];\n    var deviceType = \/iPad\/.test(navigator.userAgent) ? \"t\" : \/Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk\/.test(navigator.userAgent) ? \"m\" : \"d\";\n    window.criteo_q.push(\n        { event: \"setAccount\", account: \"&lt;?= $block->getPartnerId(); ?>\"}\n    );\n&lt;\/script>\n&lt;!-- END Criteo Sales Tag --><\/code><\/pre><\/div>\n\n\n\n<p>After that, you need to execute the following commands on the Magento server to deploy changes:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">php bin\/magento cache:flush\nphp bin\/magento setup:upgrade\nphp bin\/magento setup:di:compile\nphp bin\/magento setup:static-content:deploy\n<\/code><\/pre><\/div>\n\n\n\n<p>Once a new order is successfully created, the added scripts will send the necessary information to the affiliate program. This script can be viewed via the browser&#8217;s Dev Tools.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"337\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code-1-1-1.png\" alt=\"How to Add a Magento 2 Affiliate Tracking Code Using a Custom Module\" class=\"wp-image-1915\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code-1-1-1.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code-1-1-1-300x84.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code-1-1-1-1024x286.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code-1-1-1-768x215.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<h2 id=\"how-to-add-affiliate-tracking-code-in-magento-2-using-magento-2-affiliate-programs-extension\">2. How to Add Affiliate Tracking Code in Magento 2 Using Affiliate Programs Extension<\/h2>\n\n\n\n<p>When you manually add code to track sales through an affiliate program, you need to regularly check whether the added code continues to function correctly after an update to the affiliate network script. If the code stops working, modifications to the custom module will be necessary.<\/p>\n\n\n\n<p>This process can be quite complicated for merchants without development skills or a professional in their team. Therefore, using the <a href=\"\/magento-affiliate-programs\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 Affiliate Programs Extension<\/a> is a great solution to this problem.<\/p>\n\n\n\n<p>With this extnesion, you can add a tracking code through the admin panel. For this, go to <strong>Plumrocket -> Affiliate Programs -> Manage Affiliate Programs<\/strong> and click the Add New Affiliate button at the top.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"632\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-2.png\" alt=\"Add Affiliate Tracking Code in Magento 2 Using Magento 2 Affiliate Programs Extension: step 1\" class=\"wp-image-1854\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-2.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-2-300x157.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-2-1024x537.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-2-768x402.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<p>Then, select the required affiliate program from the list or choose the custom option if you did not find the necessary one.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"639\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-3.png\" alt=\"Add Affiliate Tracking Code in Magento 2 Using Magento 2 Affiliate Programs Extension: step 2\" class=\"wp-image-1855\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-3.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-3-300x159.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-3-1024x543.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-3-768x407.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<p>Under the General Settings tab, enable the program and select store views for which to apply it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"633\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-4.png\" alt=\"Add Affiliate Tracking Code in Magento 2 Using Magento 2 Affiliate Programs Extension: step 3\" class=\"wp-image-1856\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-4.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-4-300x157.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-4-1024x537.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-4-768x403.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<p>Go to the Affiliate Scripts tab and enter an ID or other credentials provided by your program. For example, to connect to the Criteo partner program, only the Criteo Partner ID field is required, which you can obtain on the partner program side.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"441\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-5.png\" alt=\"Add Affiliate Tracking Code in Magento 2 Using Magento 2 Affiliate Programs Extension: step 4\" class=\"wp-image-1857\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-5.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-5-300x110.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-5-1024x374.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/09\/add-affiliate-tracking-code-5-768x281.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<p>That&#8217;s it. Now, after the successful placement of the order, the pixel will fire, and the affiliate program will receive information about it from the Magento store.<\/p>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>Affiliate programs are a great way to expand the reach of your store, which benefits both the store owner and advertising platforms. Since programs mostly rely on JS tags, adding affiliate tracking code to Magento 2 can be challenging for people not familiar with technical details.<\/p>\n\n\n\n<p>This tutorial provides detailed steps to add affiliate tracking code manually by creating a custom module. As an alternative option, you can leverage <a href=\"\/magento-affiliate-programs\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 Affiliate Programs Extension<\/a>. Using the extension, a merchant can easily configure and manage active programs on the Magento store and create new ones without the help of a developer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Among different types of affiliate programs, Pay Per Sale (PPS) and Pay Per Lead (PPL) are the most commonly used. In the PPS program, the tracking code is placed on the checkout success page to confirm that the purchase was successful, while the PPL program tracks account registrations.<\/p>\n<p>Affiliate programs rely on JS tags, also known as pixels, to track conversions. However, adding them to a Magento 2 website can cause some difficulties for people who are not tech-savvy. This tutorial provides a general understanding and a detailed guide on how to add a Magento 2 affiliate tracking code to the checkout page for the Pay Per Sale program.<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[124],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Integrate Affiliate Tracking Code in Magento 2<\/title>\n<meta name=\"description\" content=\"Discover two ways to add an affiliate tracking code to Magento 2 store: by creating a custom module or automatically using an Affiliate Programs extension.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/plumrocket.com\/learn\/add-magento-affiliate-tracking-code\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Integrate Affiliate Tracking Code in Magento 2\" \/>\n<meta property=\"og:description\" content=\"Discover two ways to add an affiliate tracking code to Magento 2 store: by creating a custom module or automatically using an Affiliate Programs extension.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/plumrocket.com\/learn\/add-magento-affiliate-tracking-code\" \/>\n<meta property=\"og:site_name\" content=\"Magento Tutorials for Beginners &amp; Professionals\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-07T11:55:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-10T08:00:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/add-affiliate-tracking-code.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/1850"}],"collection":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/comments?post=1850"}],"version-history":[{"count":17,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/1850\/revisions"}],"predecessor-version":[{"id":1932,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/1850\/revisions\/1932"}],"wp:attachment":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/media?parent=1850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/categories?post=1850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/tags?post=1850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}