2 Replies Latest reply on Apr 4, 2018 1:06 PM by Sanford Whiteman

    Showing forms dynamically based on IP Address

    Jake Levinger

      Hi Marketo community,

       

      Currently, we have a US blog that sees traffic from other geos, namely Canada, that have different email laws from the US.  What we'd like to do is dynamically show a form that is compliant with the country's laws based on their IP address.

       

      The differences are slight (for Canada we want to not have "Phone" be a required field vs. requiring it in the US) so I'm thinking we could potentially do this with RTP or even modify the form code itself?  I'm not a coder nor have I ever had to tackle something like this before so I thought I'd start here before delving in

       

      Thanks,

      Jake

        • Re: Showing forms dynamically based on IP Address
          Mariah White

          Hi Jake,

           

          Another approach would be to use the form visibility rules based upon Country selection (can hide or show fields). This doesn't require custom coding and depending upon inferred data, which is nice.

           

          Best,

          Mariah

          • Re: Showing forms dynamically based on IP Address
            Sanford Whiteman

            This is either very simple (if you understand the technical approach) or what-did-just-read (if you're not a coder).

             

            The ingredients are:

             

            1. Knowledge of the person's public IP address. This is not something the browser knows on its own. Rather, you do a (lightning-fast) lookup using ipify.org and then you know the current IP.

            2. Knowledge of the Canada IP ranges. While these particular ranges change slowly, if at all, from year to year, you have to get a copy of the current ranges and set a reminder to refresh the list after 6 mos or so.

            3. A tiny bit of JS code to find out of the current IP is in the CA ranges.

             

            Once I have the decimal IP ranges loaded in JS (as the array CA_RANGES in this case) and the current public IP address in hand (that is, in browser), I use this code to determine if the person's in CA:

             

            function dottedToDecimal(dotted){
              return dotted.split(".")
                .reverse()
                .reduce(function(acc,part,idx){
                  return acc + part * Math.pow(256,idx);
              }, 0)
            }
            
            function isInDecimalIPList(list,ip){
              return list
                 .some(function(range){
                   var min = range[0],
                       max = range[1];    
                   return min <= ip && ip <= max;
                 })
            }
            
            var currentDecimalIP = dottedToDecimal(currentDottedIP);
            if ( isInDecimalIPList(CA_RANGES,currentDecimalIP) ) {
              // do something CASL-y
            }