Re: URL parameter script not working in Forms 2.0

Dan_Stevens_
Level 10 - Champion Alumni

URL parameter script not working in Forms 2.0

This resource article (https://community.marketo.com/MarketoResource?id=kA650000000GuKCCA0) provided us with the necessary guidance to properly pass URL parameters – in our case, referring URL – to our iframed forms.  But this is a forms 1.0 artilce and this no longer works in Forms 2.0.  I would expect Marketo to update this since most of us are probably using Forms 2.0.

The script is contained below and what we included on the iframe landing page:
 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!-- Keep your jQuery up to date -->
 
<script>
 
  $(function(){
    var url = (window.location != window.parent.location) ? document.referrer: document.location;
    var sourceParam = getUrlVars(url)["ref"];
    $("#Avanade_Referral_URL").val(sourceParam);
  })
   
  function getUrlVars(url) {
    var vars = [],
        hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
  }
 
</script>
 
Our contact-us page is located here: http://www.avanade.com/en-us/pages/contact.aspx

D
oes anyone have any suggestions/fixes for this?
Tags (1)
7 REPLIES 7
Kenny_Elkington
Marketo Employee

Re: URL parameter script not working in Forms 2.0

Hi Dan,

It would look something like this when using the Forms 2 API:

<script>
var myReferrer = (window.location != window.parent.location) ? document.referrer: document.location;
MktoForms2.whenReady(function(form){
form.setValues({"yourField":myReferrer});
}
</script>
Dan_Stevens_
Level 10 - Champion Alumni

Re: URL parameter script not working in Forms 2.0

Hi Kenny - so are you saying I would replace the entire script with the script you provided?  So it would look like this?:

<script>
var myReferrer = (window.location != window.parent.location) ? document.referrer: document.location;
MktoForms2.whenReady(function(form){
form.setValues({"Avanade_Referral_URL":myReferrer});
}
</script>


I don't see the "ref" query string variable reference anywhere - which is used to populate the "Avanade_Referral_URL" field.
 
 
Kenny_Elkington
Marketo Employee

Re: URL parameter script not working in Forms 2.0

That's correct, Dan.  I n that code, ref and myReferrer are equivalent, I just chose a more descriptive variable name.
Dan_Stevens_
Level 10 - Champion Alumni

Re: URL parameter script not working in Forms 2.0

Before I start testing this with this new script, I just want to validate the following: Here's a sample format of the URL structure on our site:

 
 
http://www.avanade.com/en-us/pages/contact.aspx?ref=http://www.avanade.com:80/en-us/services/pages/e...

In this case the referral URL is http://www.avanade.com:80/en-us/services/pages/enterprise-application-development.aspx

And this script below should capture this as desired:

<script>
var Ref = (window.location != window.parent.location) ? document.referrer: document.location;
MktoForms2.whenReady(function(form){
form.setValues({"Avanade_Referral_URL":Ref});
}
</script>
Kenny_Elkington
Marketo Employee

Re: URL parameter script not working in Forms 2.0

Ah I see your concern now, Dan.  The example I provided will grab the true referrer of the page, but you want tthe one which is being passed as a querystring parameter.  The snippet will need some modification:

<script>
var url = (window.location != window.parent.location) ? document.referrer: document.location;
var Ref = getUrlVars(url)["ref"];

function getUrlVars(url) {
    var vars = [],
        hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
  }

MktoForms2.whenReady(function(form){
form.setValues({"Avanade_Referral_URL":Ref});
}

</script>

Dan_Stevens_
Level 10 - Champion Alumni

Re: URL parameter script not working in Forms 2.0

Kenny, I think we're getting close here.  But right now, regardless of what the actual "ref" value is, it's always grabbing  http://www.avanade.com:80/en-us/Pages/default.aspx as the "Avanade Referral URL" value - even when the full URL (with ref query string) is http://www.avanade.com/en-us/pages/contact.aspx?ref=http://www.avanade.com:80/en-us/services/pages/c...
Dan_Stevens_
Level 10 - Champion Alumni

Re: URL parameter script not working in Forms 2.0

Kenny - to determine where the issue may lie, I decided to revert to your original, simpler script.  But even that's not working.  Here is the script:

<script>
var myReferrer = (window.location != window.parent.location) ? document.referrer: document.location;
MktoForms2.whenReady(function(form){
form.setValues({"Avanade_Referral_URL":myReferrer});
}
</script>

For the hidden form field, I defined it as follows - is this correct?  

0EM50000000S3Af.jpg

And finally, when viewing the source code of the landing page, the script appears after the form.  Does this make a difference?

Thanks!