Understanding what a javascript code snippet does

Could someone please explain what exactly this javascript code does

<script type="text/javascript">
$(document).ready(function(){
var fullstring = getParameterByName("redirect");
var subdomainarray = fullstring.split("/");
var sub_domain_url = subdomainarray[2];
var img_hit = '<iframe src="http://4520586.fls.doubleclick.net/activityi;src=4520586;type=Freet0;cat=FreeT0;qty=1;u1=['+sub_domain_url+'];cost=[Revenue];ord=[OrderID]?" width="1" height="1" frameborder="0" style="display:none"></iframe>';
$("#doubleclick_img_pixel").append(img_hit);
});
</script>
<div id="doubleclick_img_pixel"></div>

Open in new window


What does this code do? Can someone please explain?

This is the website:
freshdesk.com

The process is that users go to the landing page and sign in, then they get here: freshdesk.com/signup/thank-you

That is where the floodlight conversion pixel /tag is placed for tracking signups or conversions.

When someone signups, a unique "thank you" page is generated, here is an example:
http://freshdesk.com/signup/thank-you?redirect=http%3A%2F%2Ftestcompanyname1.freshdesk.com%2Fsignup_complete%2FndPAXFOfs7TEYnbE5WsC&account=testcompanyname1
smuralisankarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Basically the code takes a parameter from the URL (redirect) and splits it into its domain / subdomain components.

It then creates an iframe with the subdomain as parameter in the iframe src. It then appends this iframe element to the <div> with id="doubleclick". Full commented code below
<script type="text/javascript">
$(document).ready(function(){
  // APPEARS TO BE A CALL TO A CUSTOM FUNCTION
  // I ASSUME THE FUNCTION RETURNS A PARAMETER
  // FROM THE URL - IN THIS CASE redirect 
  var fullstring = getParameterByName("redirect");
  
  // SPLIT THE STRING INTO AN ARRAY OF STRINGS
  // USING THE / AS THE DELIMITER
  // ASSUMING FROM THE NAME THAT THE PARAMETER
  // VALUE IS A DOMAIN PATH
  var subdomainarray = fullstring.split("/");
  
  // GET THE THIRD ELEMENT IN THE ARRAY. IF
  // STRING INPUT WAS
  // something.com/somethingelse/yetanotherthing
  // THEN sub_domain_url WOULD BE SET TO
  // yetanotherthing
  var sub_domain_url = subdomainarray[2];
  
  // CREATE AN <iframe> DEFINITION AND SET
  // THE u1 PARAMETER IN THE src TO THE
  // VALUE RETRIEVED ABOVE
  var img_hit = '<iframe src="http://4520586.fls.doubleclick.net/activityi;src=4520586;type=Freet0;cat=FreeT0;qty=1;u1=['+sub_domain_url+'];cost=[Revenue];ord=[OrderID]?" width="1" height="1" frameborder="0" style="display:none"></iframe>';
  
  // APPEND THE <iframe> TO THE <div> WITH
  // id="doubleclick"
  $("#doubleclick_img_pixel").append(img_hit);
});
</script>
<div id="doubleclick_img_pixel"></div>

Open in new window

The style of the iframe is set to dispaly: none - so the iframe does not appear to be doing anything other than making a call back to doubleclick.net to record a page view - probably part of an affiliate tracking process
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smuralisankarAuthor Commented:
We are using DFA / DCM (Doubleclick for advertisers / Doubleclick campaign manager) as our adserver. We need to passback a unique ID for each conversion that we get.

We don't generate an order ID or transaction ID for each signup but we do generate a unique thank you page URL for each signup.

This is the thank you page where we would have the DFA conversion tag:
freshdesk.com/signup/thank-you

This is the unique thank you page URL that would be generated for each signup (This is just for one account):
http://freshdesk.com/signup/thank-you?redirect=http%3A%2F%2Ftestcompanyname1.freshdesk.com%2Fsignup_complete%2FndPAXFOfs7TEYnbE5WsC&account=testcompanyname1

The javascript code snippet that I posted would work on this URL.

Could you explain how that would happen please?
0
Julian HansenCommented:
Lets break it down.

First the redirect parameter - from the URL you gave it would be

redirect=http://testcompanyname1.freshdesk.com/signup_complete/ndPAXFOfs7TEYnbE5WsC

Open in new window


so the first line of code would be to assign this parameter to the fullstring value
var fullstring = getParameterByName("redirect");

Open in new window


Next we find the sub domain array
var subdomainarray = fullstring.split("/");

Open in new window

The value of subdomainarray is now
[0] => 	"http:"
[1] => 	""
[2] => 	"testcompanyname1.freshdesk.com"
[3] => 	"signup_complete"
[4] => 	"ndPAXFOfs7TEYnbE5WsC"

Open in new window

Next we create the iframe
var img_hit = '<iframe src="http://4520586.fls.doubleclick.net/activityi;src=4520586;type=Freet0;cat=FreeT0;qty=1;u1=['+sub_domain_url+'];cost=[Revenue];ord=[OrderID]?" width="1" height="1" frameborder="0" style="display:none"></iframe>';  

Open in new window

Which - after the parameter insertion would come out as

http://4520586.fls.doubleclick.net/activityi;src=4520586;type=Freet0;cat=FreeT0;qty=1;u1=[testcompanyname1.freshdesk.com];cost=[Revenue];ord=[OrderID]?

Open in new window


The iframe is then added to the document which results in the url being called.

As you can see from the above this is not going to result in a unique URL. I am assuming that the last part of the the thank you page is this?
ndPAXFOfs7TEYnbE5WsC

Open in new window


You could use that as your OrderID and change the src parameter on the iframe to
var sub_domain_url = subdomainarray[2];
var order_id = subdomainarray[4];  
var img_hit = '<iframe src="http://4520586.fls.doubleclick.net/activityi;src=4520586;type=Freet0;cat=FreeT0;qty=1;u1=['+sub_domain_url+'];cost=[Revenue];ord=[' + order_id + ']?" width="1" height="1" frameborder="0" style="display:none"></iframe>';

Open in new window


But not knowing enough about your setup cannot guarantee this is correct.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

smuralisankarAuthor Commented:
1.
testcompanyname1.freshdesk.com is the unique ID.

When someone signups, they enter "testcompanyname1" for their account sub domain or account name.

freshdesk.com/signup - see that. It is our signup page. You can sign up and see for yourself.

No one can enter a sub-domain if already exists.

So, the url is going to be unique for each account or signup or conversion.

2.
I am not a tech guy so I have trouble understanding this.
Everything you have explained is fine.
So, the subdomain URL is now stored to a variable called u1?

How would that variable be picked up and passed into DFA (our adserver)?
0
Julian HansenCommented:
So, the url is going to be unique for each account or signup or conversion.
I understand - if the sub-domain is unique then yes the request will be unique and no other code is necessary (in theory - depends on how DA interprets the request)

In answer to the second question - the iframe is how the information is passed. The iframe acts like a browser. When you set the src attribute and then attach the <iframe> to the <div id="doubleclick"> this results in the url that is in the src attribute being loaded into the iframe - in other words the url is executed.

So, the subdomain URL is now stored to a variable called u1?

Yes
0
smuralisankarAuthor Commented:
So, the subdomain URL is now stored to a variable called u1? - yes

1. Where exactly is it stored?
2. I don't understand how that is picked up by the adserver?
0
Julian HansenCommented:
1. In the URL
http://4520586.fls.doubleclick.net/activityi;src=4520586;type=Freet0;cat=FreeT0;qty=1;u1=[testcompanyname1.freshdesk.com];cost=[Revenue];ord=[OrderID]?

The subdomain gets inserted into the url string as above - refer the part in bold                                          

2. An iframe is an in page element that acts as a browser. For instance

<iframe src="http://www.experts-exchange.com"></iframe>

Open in new window

This will create an instance of EE in the page. For example
<!doctype html>
<html>
<body>
<p>This is before the IFrame</p>
<iframe src="http://www.experts-exchange.com"></iframe>
<p>This is after the IFrame</p>
</body>
</html>

Open in new window

Creates this page
Screen shotAs you can see there is an instance of EE in the middle of the page - the URL that is the src attribute of the IFrame was called to create this in page element.
In your case the same thing happens except the IFrame is hidden. The SRC attribute of the IFRame calls back to the Adsever server with the parameters shown - they pickup the parameters from the HTTP request - done.
0
smuralisankarAuthor Commented:
How was the screenshot taken? I did you test out the iframe?
0
Julian HansenCommented:
I took a screen grab of the browser.

I did you test out the iframe?
Not sure what the question is asking.

The iframe is exactly the same as if you cut and pasted the URL into your browser - it calls the page on the URL with the parameters  given - once that happens the process on the server can retrieve the parameters and do with them whatever needs to be done.
0
Julian HansenCommented:
The example was meant to demonstrate how an <iframe> works - in your case the iframe is hidden because its primary function is to send parameters to your click monitoring service
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Internet Marketing

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.