PDA

View Full Version : Stuck, Please Help.



ruby2212
01-28-2009, 03:41 PM
Hi All You Experts,

Could someone please help me??
I have written a function which loads in the body of an asp page and checks to see if a user has a cookie already set and it if doesnt then it sets it and writes out some more javascript code (google analytics).

I have got the whole setting/retrieveing cookie bit sorted its just the writing of the additional javascript google analytics code. I have tried document.write but that doesn't help.

The function is:

function testFunction(){
var c = getCookie(val);
if(!c){
alert('Setting New Cookie\nName :' + val);
setCookie(val, 'tracked', 1000);
}
else{
document.write('cookie set');
}

}

I would like the following code to be written straight after the setCookie function is called:

<!-- Google Code for Lead Conversion Page -->
<script language="JavaScript" type="text/javascript">
<!--
var google_conversion_id = 1098528816;
var google_conversion_language = "en_GB";
var google_conversion_format = "1";
var google_conversion_color = "FFFFFF";
if (100.0) {
var google_conversion_value = 100.0;
}
var google_conversion_label = "Lead";
//-->
</script>
<script language="JavaScript" src="http://www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<img height="1" width="1" border="0" src="http://www.googleadservices.com/pagead/conversion/1098528816/?value=100.0&label=Lead&script=0">
</noscript>

How would I go about doing this. Any help??

Thanks

codeexploiter
01-29-2009, 04:13 AM
I have changed your code a bit, mainly avoided the use of document.write.

Insert the following code in your page, which will be trigger as a part of the 'onload' event of 'window' object. Please note that I have placed a window.onload as the last part of my code below, if you have any code that fires as a part of the 'onload' event of 'window' object, make sure that you remove them from their place and insert it inside my function.



<script type="text/javascript">
function testFunction(){
var c = getCookie(val);
if(!c){
alert('Setting New Cookie\nName :' + val);
setCookie(val, 'tracked', 1000);
setGoogleAnalytics();
}else{
}
}
function setGoogleAnalytics(){
var google_conversion_id = 1098528816;
var google_conversion_language = "en_GB";
var google_conversion_format = "1";
var google_conversion_color = "FFFFFF";
if (100.0) {
var google_conversion_value = 100.0;
}
var google_conversion_label = "Lead";

var script = createElement('script',{type:"text/javascript",src:"http://www.googleadservices.com/pagead/conversion.js"});
var noscript = createElement('noscript');
var img = createElement('img',{height:"1",width:"1",border:"0",src:"http://www.googleadservices.com/pagead/conversion/1098528816/?value=100.0&label=Lead&script=0"});
if(noscript && script && img){
noscript.appendChild(img);
document.body.appendChild(script);
document.body.appendChild(noscript);
}else{
alert('The element creation failed');
}
}
function createElement(tag,props){
if (typeof tag !== 'undefined' && tag.replace(/^\s+|\s+$/g,'') !== ''){
var el = document.createElement(tag);
if(typeof props === 'undefined'){
return el;
}
for(var item in props){
el[item] = props[item];
}
return el;
}
}

window.onload = function(){
testFunction();
}
</script>


Hope this helps.

ruby2212
01-29-2009, 09:50 AM
Hey CodeExploiter, thanks for your quick reply.

However it's still not working, the script tag that you created and appended is supposed to download something from Google and display it but it doesn't. It's how it does the tracking of transactions.

That's the problem I had with it in the beginning.

Any other ideas???

Thanks for trying.