Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CONTINUATION - calling a url outside of javascript function??

Status
Not open for further replies.

willronin

Programmer
Dec 17, 2010
1
US
thread216-1057021

hello world : ), Willronin here, this is my first post in this forum!

I came across the above thread recently:
in case you can not find it here is the URL of the tread I am talking about:


I felt the tread originator's frustration at getting answers that where not addressing his question instead where answers completely off the amrk or attempted to open discussion on the manipulation of google code.
StormBind posted a couple of guestimates that did not address the problem in question and hopes for the best.. nice try, no dice!
then Kahn comes in and flames the poster for getting frustrated at not getting an answer!!! yes you are right no one here gets pay for helping another person and people should be gracious and thankful when getting help from others, BUT that does not give you/us the liberty to completely disregard and not attempt to at least understand the issue in question. - understand the question before providing an answer, or at least reach out and ask for some sort of clarification. don't just throw an answer out there and hope it sticks.

now for waht I hope is the right answer, and maybe some other unfortunate weary web traveler doing battle with the DOM beast of the world wide web will find in this little corner of the web a sanctuary and respite form her or his journey.

google tracking code as its given by google:

<!-- Google Code for Lead Conversion Page -->
<script language="JavaScript" type="text/javascript">
<!--
function googleAdword(){
var google_conversion_id = **********;
var google_conversion_language = "en_GB";
var google_conversion_format = "1";
var google_conversion_color = "666666";
if (0) {
var google_conversion_value = 0;
}
var google_conversion_label = "Lead";
//-->
}
</script>
<script language="JavaScript" src="</script>

what our unfortunate poster was asking was:
how can he prevent conversion.js from being triggered on page load?
he was asking how could he place the conversion.js inside a function which he could trigger from within a flash button?

Below we will deliver what we hope is the correct solution.
After that we will explain the solution, so that others attempting to implement the solution can better trouble shoot it and hopefully add to this Post and thereby increasing our collective knowledge.

place this inside the header or somewhere within the body of your HTML document.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-15586217-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? ' : ' + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

function injTrckPxls ()
{
/* this is google code parameters - please do not change - you may extend the declarations. */
_gaq.push(['_trackEvent', 'declaration1', 'declaration2', 'declaration3']);
/* begin pixel code injection */
document.getElementById('declaration1').innerHTML = 'WRITE YOUR HTML IN HERE';
document.getElementById('declaration2').innerHTML = 'WRITE YOUR HTML IN HERE';
document.getElementById('declaration3').innerHTML = 'WRITE YOUR HTML IN HERE';
}
</script>

create some divs to receive the HTML form the javascript.
normally this elements are placed aright before the closing </body> tag,
<div id="declaration1"></div>
<div id="declaration2"></div>
<div id="declaration3"></div>

so what are we doing.
simple we have a function called injTrckPxls
this function needs to be called from the flash movie.
if not called no tracking will occurred.
when triggered the functiom will inject WHATEVER code you place in the innerHTML declaration
this could be iframe, and image, another div, or another javascript if so we desire.
we are tunneling thru the DOM via document once we are in document we search the innerHTML and we look for our declarations' ID via getElementByID once find it we write our own html snippet.

in the posters' case the correct way to place the conversion.js would be
document.getElementById('declaration3').innerHTML = '<script type="text/javascript" src="
so that when his flash button is clicked, the conversion.js is injected into the html page.
this approach can also be use to inject pixel tracking code that resides in a img src tag or in an iframe.

hope this helps someone facing the same issue, remember we are not experts, we are hobbist we a passion for coding.

Willronin
 
I commend you for coming in and providing an answer. I'd also like to point out that it is a 5 and a half year old thread.


how can he prevent conversion.js from being triggered on page load?

I don't believe this is what he was asking, I think what he wanted to ask is what he could do with that section which is out side the function he wants to call. What he didn't realize is that that portion simply brings in the functions that the google adwords function is calling.

So the answer to his original question:
he second piece of javascript google supply has the url to the script on their server but I don;t know how to encapsulate this also into the function

Is that he doesn't really need to encapsulate it at all. Just stick it right there under his other Js functions period.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
After looking at the code in "conversion.js", it appears that as soon as the file is included, it runs and expects to find the variables that are set up in the "googleAdword" function.

So, I think elvizcat wanted to be able to include the <script> at a later stage as part of his function rather than not have the function run immediately.

However, unless I've completely mis-read Google's code, I'd say there is a problem with being able to do this. Take this portion of the code that runs when "conversion.js" is included (the formatting is mine):

Code:
var A;
if (x.google_conversion_type == "landing" || !x.google_conversion_id)
	A = false;
else {
	x.google_conversion_date = new Date;
	x.google_conversion_time = x.google_conversion_date.getTime();
	if (typeof x.google_conversion_snippets == "number" && x.google_conversion_snippets > 0)
		x.google_conversion_snippets += 1;
	else
		x.google_conversion_snippets = 1;

	if (typeof x.google_conversion_first_time != "number")
		x.google_conversion_first_time = x.google_conversion_time;

	x.google_conversion_js_version = "6";

	if (x.google_conversion_format != 0 && x.google_conversion_format != 1 && x.google_conversion_format != 2 && x.google_conversion_format != 3)
		x.google_conversion_format = 1;

	A = true
}

A && document.write(w(x, navigator, document))

The global variable "google_conversion_type" isn't set (that we know of), and the global variable "google_conversion_id" is set. This means the first test will fall through to the else block, ultimately setting A to true.

With this being the case, the line immediately following the else block will execute a document.write call, which, if called after the document has loaded, has the unfortunate effect of clearing everything in the existing document - probably not what elvizcat desired. For this reason, I'll stick my neck on the line and say that what he wanted done cannot be easily done with Google's code as it stands.

I say 'easily', as there are tricks such as running the code inside an <iframe> dedicated to hosting the ads (thus is doesn't matter if its document is wiped), or temporarily hijacking document.write for the duration of the call. While both of these would work for elvizcat, I'd certainly not recommend the latter to anyone not au fait with JavaScript.

So, I'd advise anyone wanting to do the same thing to use an <iframe> element to host your adverts if you want to load them post-page load instead of inline... in fact, some of Google's newer ad code does this automatically (whether for the same reason or not we can only speculate).

Willronin - I also commend you for providing an answer, even for such an old post - it's these answers that keep Tek-Tips such an active site.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Hmm... the typo in my second sentence unfortunately changes its meaning. Imagine the word "not" is not there when reading this sentence:
me said:
So, I think elvizcat wanted to be able to include the <script> at a later stage as part of his function rather than not have the function run immediately.
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top