3

I am trying to insert a Google Remarketing code into the Javascript function of the CookieCuttr, in order to block cookies.

I use the CookieCuttr code to block cookies, and the function is as follows:

<script>
function cookiestart(){
[ ... ]
}

if (jQuery.cookie('cc_cookie_accept') == 'cc_cookie_accept') {

HERE GOES THE GOOGLE ANALYTICS CODE

}
</script>

This works perfectly: Google Analytics cookies are blocked until the visitor accepts them. Now, I have to include in the...

<script>
[...]
</script>

...section the Google Remarketing code:

<!-- Google Code per il tag di remarketing -->
<!--------------------------------------------------
I tag di remarketing possono non essere associati a informazioni di     identificazione personale o inseriti in pagine relative a categorie sensibili.     Ulteriori informazioni e istruzioni su come impostare il tag sono disponibili     alla pagina: http://google.com/ads/remarketingsetup
--------------------------------------------------->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 123456789;
var google_custom_params = window.google_tag_params;
var google_remarketing_only = true;
/* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/123456789/?value=0&amp;guid=ON&amp;script=0"/>
</div>
</noscript>

Is there a way to include this code in the previous one?

Thank you everybody.

Report
  • 31
  • 4

1 Answers1

0

This should do what you needed. It's not obvious when your code is being called, so this waits until the document is loaded to add the analytics code. This is based on answers in Can I add javascript dynamically to an existing script element

document.addEventListener("DOMContentLoaded", function() {
    var script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    script.appendChild(document.createTextNode("\
/* <![CDATA[ */\n\
var google_conversion_id = 123456789;\n\
var google_custom_params = window.google_tag_params;\n\
var google_remarketing_only = true;\n\
/* ]]> */\n\
"));
    document.body.appendChild(script);
    script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", "//www.googleadservices.com/pagead/conversion.js");
    document.body.appendChild(script);
    var noscript = document.createElement("noscript");
    noscript.innerHTML =
'<div style="display:inline;">\
<img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/123456789/?value=0&amp;guid=ON&amp;script=0"/>\n\
</div>';
    document.body.appendChild(noscript);
}, false);
Community
  • 1
  • 1
Tibrogargan
  • 4,508
  • 3
  • 19
  • 38