Targeted Fastlane Script Per Site Language

Serving FastLane based on the site language can be easier than it sounds. The magic is in the proper statement that evaluates which language the site is served in and then chooses the proper playbook that matches the language. Here is what we mean by that:

1- Define your full list of languages

In this step, you want to list out your site's languages and the correlated playbook campaignId.

// Call out the default campaignId that will catch all site visitors
var campaign_id = 2511234;

// Using jQuery to locate the site's region [served language]
var dataRegion = jQuery('html').attr('data-region');

      switch (dataRegion) {
        case 'de':
          campaign_id = 2520192;
          break;
     
        case 'fi':
          campaign_id = 2520987;
          break;
 
     
        default:
          campaign_id = 2511234;
          break;
      }

2- Structure your if statement

In this sample we are:

  • Checking the base language.
  • Apply on submit trigger.
  • Add a value condition [form-specific in this test] for the number of employees under 2 to be excluded.
  • Make sure drift is enabled.
  • Prevent the form from being submitting using [event.preventDefault()].
  • Loading Fastlane.
if ( dataRegion != 'gb' || dataRegion != 'es') {
    
    
            var driftform = '.test-form';
    
            jQuery( '.test-form .button-submit' ).on( 'click', function(event) {
            if(jQuery('.test-form').find('select[name="numemployees"]').val() != '0-2' ) {
                if(jQuery('.test-form').hasClass('drift-enabled')) {
                      return true;
                } else {
                jQuery('.test-form').addClass('drift-enabled');
                event.preventDefault();
                }
    
              window.drift.api.collectFormData(driftform, {
              campaignId: campaign_id,
              });
            }
    
            });
        }

Full Script

// Call out the default campaignId that will catch all site visitors
var campaign_id = 2511234;

// Using jQuery to locate the site's region [served language]
var dataRegion = jQuery("html").attr("data-region");

switch (dataRegion) {
  case "de":
    campaign_id = 2520192;
    break;

  case "fi":
    campaign_id = 2520987;
    break;

  default:
    campaign_id = 2511234;
    break;
}
// Do not fire if the site language is in English or Spanish
if (dataRegion != "gb" || dataRegion != "es") {
  var driftform = ".test-form";

  jQuery(".test-form .button-submit").on("click", function (event) {
    // An extra if statement to trigger FastLane only based on a numemployees that is over 2 employees
    if (
      jQuery(".test-form").find('select[name="numemployees"]').val() != "0-2"
    ) {
      if (jQuery(".test-form").hasClass("drift-enabled")) {
        return true;
      } else {
        jQuery(".test-form").addClass("drift-enabled");
        event.preventDefault();
      }

      window.drift.api.collectFormData(driftform, {
        campaignId: campaign_id,
      });
    }
  });
}