Solved

Understanding what a javascript code snippet does

Posted on 2014-10-29
10
137 Views
Last Modified: 2014-11-10
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
0
Comment
Question by:smuralisankar
  • 6
  • 4
10 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40410593
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
 

Author Comment

by:smuralisankar
ID: 40410750
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
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 40410832
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
 

Author Comment

by:smuralisankar
ID: 40411069
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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40411141
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:smuralisankar
ID: 40411471
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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40411945
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
 

Author Comment

by:smuralisankar
ID: 40412035
How was the screenshot taken? I did you test out the iframe?
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40412601
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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40412603
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Read about how to choose the best possible content marketing agency to suit your needs. Content marketing has become an integral part of running a successful tech business, so it is wise to be informed.
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now