0

I'm trying to include this Regex condition and activate the analytics code (if it meets the Regex condition) in a javascript function posted below.

if (/^[abc].*/i.test(mystring)) {

   var _gaq = _gaq || [];  
   _gaq.push(['_setAccount', 'UA-XXXXXXX-XX']); 
   _gaq.push(['_trackPageview']);  
        (function() {  
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;  
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';  
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  
        })(); 

       alert('Congrats ABC!');  
}



Now how can I add the code above (regex condition and the analytics) in there?
Also, please note. The portion below works perfectly at the moment.

function validateCoupon( form ){
    var mystring = document.getElementById('textCoupon').value; 

if( form.textCoupon.value.length )
        {
    // validate coupon
    $.get( "/include/checkItOut.php", { coupon: form.textCoupon.value }, validateShipping );
    alert('Performed the check and matched');
        }
else
{
    form.textCoupon.style.border = '';
    validateShipping( "yes" );

    alert('You Did Not Use Any Code');
}

return false;
}


In other words. Somehow include the Regex condition and analytics trigger to this function here.

detonate
  • 289
  • 2
  • 11
  • 21
  • 5
    what does "Now how can I add the code above in there?" mean? – Murali VP Mar 11 '11 at 16:50
  • I'm trying to include the portion if (/^abc$/i.test... in the code below without success. It might be a syntax or condition issue. Any suggestions on this? – detonate Mar 11 '11 at 17:00
  • 1
    In `if (/^abc$/i.test(mystring);) {`, remove the `;` from inside the `if()`. – gen_Eric Mar 11 '11 at 17:05
  • (/^[abc].*/i.test(mystring)) changed it like so. It needs to check and make sure it matches ABC. Is this correct? – detonate Mar 11 '11 at 17:10
  • What do you actually want? Call the analytics code if `validateCoupon` is called? `mystring` is not included in the function below, what should it do? Matching it against the coupon code? – Lekensteyn Mar 11 '11 at 17:11
  • Thanks for noticing this Lekensteyn, I added a var at the beginning of the code function. Still does not work for me yet though. `var mystring = document.getElementById('textCoupon').value; ` – detonate Mar 11 '11 at 17:19
  • what exactly are you trying to do? are you trying to let google analytic know if a coupon code is successful or failed? – KJYe.Name Mar 11 '11 at 17:21
  • kjy112, if that specific coupon code meets the Regex condition, it triggers analytics. If other coupon codes are successful (outside the regex condition) it's still ok but does not need the analytics to be activated. – detonate Mar 11 '11 at 17:27
  • Your regex is not correct to match ABC. In that case it should be /^abc.*$/i - Omit the brackets, as that only matches one character – Eric Wendelin Mar 11 '11 at 17:30
  • @detonate so the goal is to track coupon code enter success rate right? why not use custom variable in google analytics? – KJYe.Name Mar 11 '11 at 17:34
  • `/^[abc].*$/i` matches any code starting with `a`, `b` or `c` (or their uppercase versions). Do you really want that? If your coupon code starts with `abc` (uppercase allowed), followed by 5 till 8 digits, use `/^abc\d{5,8}$/i`. – Lekensteyn Mar 11 '11 at 18:40
  • kjy112, this could be a good suggestion. How would I approach this if my button trigger is `
    `
    – detonate Mar 11 '11 at 18:46
  • Lekensteyn, just need to check for "abc" lower or uppercase, no other characters after. How would you format `/^abc\d{5,8}$/i` to accommodate this? – detonate Mar 11 '11 at 18:48

1 Answers1

0

A literal comparison between the coupon code (in lower case) and abc using the equality operator (==) is enough. You should consider a server side solution, everyone can open the source and fetch the coupon code "abc" from it.

function validateCoupon( form ){
    var mystring = document.getElementById('textCoupon').value; 

if( form.textCoupon.value.length )
        {
    // added: Analytics
    if ( form.textCoupon.value.toLowerCase() == "abc") {
      var _gaq = _gaq || [];  
      _gaq.push(['_setAccount', 'UA-XXXXXXX-XX']); 
      _gaq.push(['_trackPageview']);  
      (function() {  
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;  
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';  
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  
      })();
    }
    // end analytics
    // validate coupon
    $.get( "/include/checkItOut.php", { coupon: form.textCoupon.value }, validateShipping );
    alert('Performed the check and matched');
        }
else
{
    form.textCoupon.style.border = '';
    validateShipping( "yes" );

    alert('You Did Not Use Any Code');
}

return false;
}
Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
  • Once again, been caught banging my head on syntax and this now works like a charm Lekensteyn! I'm a designer, can I help you an any way in return? – detonate Mar 11 '11 at 19:55
  • Just one question though, will the analytics take action every time or only when abc is correctly answered in this answer? – detonate Mar 11 '11 at 19:56
  • @detonate: if the couponcode equals `abc` (case-insensitive), analytics will be called. Once again, this approach is *insecure*. It looks like noobish implementations of a "login system" in Javascript: `if(password=="apple")/*ACCESS GRANTED*/`. (it can be found in the source code) Even if you use some kind of hashing, the coupon code can still be brute-forced, given the fact that coupon codes are generally not that long. But, if you do not care if the coupon code is visible, this code is fine. – Lekensteyn Mar 11 '11 at 20:40
  • Good suggestion! Could probably look into MD5 or such. – detonate Mar 11 '11 at 21:09
  • It depends on your usage. If you are selling clothing and the coupon code gives you 50% discount, you probably do not want to put it on the page at all. If the code gives you a free button, it does not matter of course. For a fast Javascript implementation of MD5, look at [this SO question](http://stackoverflow.com/q/1655769/427545). Use some salt to make table lookups and brute forcing (a bit) more difficult. – Lekensteyn Mar 11 '11 at 21:22
  • 1
    Thanks for all the help Lekensteyn! – detonate Mar 11 '11 at 21:27