title: Issue with GTM and firing Facebook Pixel in a cross-domain setup
body: <p><strong>I am trying to track Facebook ad results using the Facebook Pixel during appropriate events</strong> <em>(page views, lead generation, order form view, purchase)</em>. I can do all of this for GA using GTM with no problem, but on Facebook I only have partial success.</p>
  <h1>The main issue is I have a cross domain setup as shown below:</h1>
  <ol>
  <li><strong>domain1.com/offer</strong> - landing page <em>(FB Page View Pixel should fire)</em></li>
  <li><strong>domain1.com/ordergate</strong> - request email before showing order form page (FB Page View Pixel should fire)</li>
  <li><strong>crm.com/formsubmission</strong> - the actual form submits to my crm <em>(FB Lead Pixel should fire)</em></li>
  <li><strong>crm.com/orderform</strong> - order form <em>(FB order form view pixel should fire)</em></li>
  <li><strong>domain1.com/thankyou</strong> - the thank you page <em>(FB order pixel should fire)</em></li>
  </ol>
  <p>So my trigger on GTM to fire FB pixel was the &quot;referrer&quot; containing &quot;facebook&quot;. <strong>However, because of the multi-step process, the referrer is lost by the time the order form or sale is completed.</strong></p>
  <h1>I have since then learned I need to do the following:</h1>
  <ol>
  <li>User lands from facebook, write cookie with an appropriately short expiration time that stores this information on domaiin1.com.</li>
  <li>When the user clicks a link and is redirected to crm.com, check if the user has the cookie, and if they do, add something like ?reffacebook=true to the redirect URL.</li>
  <li>On crm.com, if the URL has ?reffacebook=true write the same cookie you wrote on (1) with an equally short expiration time.</li>
  </ol>
  <h1>UPDATE</h1>
  <p>So I have figured out step 2 using the following script on page view when the Facebook cookie is set:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function updateLinks(parameter, value)
  {
  
     var links = document.getElementsByTagName('a');
     var includeDomains = self.location.host;
     
     for (var i=0;i&lt;links.length;i++)
     {
         if(links[i].href != "#" &amp;&amp; links[i].href != "/" &amp;&amp; links[i].href != "" &amp;&amp; links[i].href != window.location) //Ignore links with empty src attribute, linking to site root, or anchor tags (#)
         {
             var updateLink = true;           
             
             if(links[i].href.toLowerCase().indexOf(includeDomains.toLowerCase()) != -1) //Domain of current link is included i the includeDomains array.  Update Required...
             {
               updateLink = false;
             }           
         
             if(!updateLink)
             {
                 //Do nothing - link is internal
             }
             else
             {
                 var queryStringComplete = "";
                 var paramCount = 0;
             
                 var linkParts = links[i].href.split("?");
                 
                 if(linkParts.length &gt; 1) // Has Query String Params
                 {
                     queryStringComplete = "?";
                 
                     var fullQString = linkParts[1];
                     var paramArray = fullQString.split("&amp;");    
                     var found = false;
                     
                     for (j=0;j&lt;paramArray.length;j++)
                     {
                         
                         var currentParameter = paramArray[j].split("=");
                         
                         if(paramCount &gt; 0)
                             queryStringComplete = queryStringComplete + "&amp;";
                         
                         if(currentParameter[0] == parameter) //Parameter exists in url, refresh value
                          {
                              queryStringComplete = queryStringComplete + parameter + "=" + value;
                              found = true;
                          }
                          else
                          {
                              queryStringComplete = queryStringComplete + paramArray[j]; //Not related parameter - re-include in url
                          }
                         
                          paramCount++;
                     }
                     
                     if(!found) //Add new param to end of query string
                         queryStringComplete = queryStringComplete + "&amp;" + parameter + "=" + value;
                 }
                 else
                 {
                     queryStringComplete = "?" + parameter + "=" + value;
                 }
                     
                 links[i].href = links[i].href.split("?")[0] + queryStringComplete;        
             }
         }
         else
         {
             //Do nothing
         }
     }
  }</code></pre>
  </div>
  </div>
  </p>
  <p>So with this code I can now properly attribute people with the facebook referral across domains...</p>
  <p>...but I still have a problem with form submits.</p>
  <p>So when the contact gets to step 4, it is a redirect from the form submission. It does not carry any cookie or query string, so neither of the FB pixels (order form view or order) is being fired.</p>
  <p>I'm not sure how I would handle this. My first thought is to pass a hidden field into the form submission (say reffacebook=true). Then somehow expose that in the url in a form of a query string so that it can be detected by GTM.</p>
  <p>This seems to be somewhat complicated though, as I would have to edit all my forms to have this variable, edit my CRM so it knows to receive it, and then edit the form landing page to expose that variable in the url.</p>