rudderlabs / community-user-transformations

MIT License
1 stars 1 forks source link

Mark qualified lead #11

Open gitcommitshow opened 1 year ago

gitcommitshow commented 1 year ago

Contact Details

psharma@rudderstack.com

Language

Javascript

Category

Data Security & Governance

Description

This transformation marks leads whether they are qualified leads or not

Code Block

/**
 * Tag leads whether they are qualified leads or not
 * For a lead to qualify, it has to satisfy two aspects 
 * 1. Intent to buy (e.g. event == 'Clicked on product detail page') 
 * 2. Their company metrics match ideal customer profile (e.g. US company with more than 250 employees)
 */
const CLEARBIT_API_KEY = "<your Clearbit API key>";
/**
 * Criterias for qualified lead
 */
const EVENTS_WITH_INTENT_TO_BUY = ["Request Quote", "Product Clicked"]
const MIN_EMPLOYEE_COUNT = 250;
const QUALIFIED_BUSINESS_LOCATIONS = ['US'];

export async function transformEvent(event) {
    const email = event.context?.traits?.email;
    const domain = email.split("@")[1];
    if(!EVENTS_WITH_INTENT_TO_BUY.includes(event.event) || !domain){
        // This is not the event with intent to buy or has an invalid email
        return event;
    }
    try{
        // Get company info
        const companyInfo = await fetch("https://company.clearbit.com/v2/companies/find?domain=" + domain, {
            headers: {
                "Authorization": "Bearer "+CLEARBIT_API_KEY
            }
        });
        log(JSON.stringify(companyInfo, null, 4))
        if(!companyInfo.metrics || !companyInfo.metrics.employees || !companyInfo.geo){
            // Can't say anything if there's not enough info about the company
            event.context.traits.isQualifiedLead = "🤷";
        } else if(isQualifiedCompany(companyMetrics)) {
            // This is a qualified lead
            event.context.traits.isQualifiedLead = "👍";
        } else {
            event.context.traits.isQualifiedLead = "👎";
        }
    } catch(err){
        log("Error in finding company info via Clearbit APIs")
        log(err)
    }
    return event;
}

/**
 * Function that returns true if company metrics match qualified lead criteria
 */
function isQualifiedCompany(companyInfo){
    if(companyInfo.metrics.employees < MIN_EMPLOYEE_COUNT){
        return false;        
    }
    if(!QUALIFIED_BUSINESS_LOCATIONS.includes(companyInfo.geo.countryCode)){
        return false;
    }
    return true;
}

Input Payload for testing

[
  {
    "anonymousId": "8d872292709c6fbe",
    "channel": "mobile",
    "context": {
      "app": {
        "build": "1",
        "name": "AMTestProject",
        "namespace": "com.rudderstack.android.rudderstack.sampleAndroidApp",
        "version": "1.0"
      },
      "device": {
        "id": "8d872292709c6fbe",
        "manufacturer": "Google",
        "model": "AOSPonIAEmulator",
        "name": "generic_x86_arm",
        "type": "android"
      },
      "library": {
        "name": "com.rudderstack.android.sdk.core",
        "version": "1.0.2"
      },
      "locale": "en-US",
      "network": {
        "carrier": "Android",
        "bluetooth": false,
        "cellular": true,
        "wifi": true
      },
      "os": {
        "name": "Android",
        "version": "9"
      },
      "screen": {
        "density": 420,
        "height": 1794,
        "width": 1080
      },
      "timezone": "Asia/Kolkata",
      "traits": {
        "address": {
          "city": "Kolkata",
          "country": "India",
          "postalcode": "700096",
          "state": "West bengal",
          "street": "Park Street"
        },
        "age": "30",
        "anonymousId": "8d872292709c6fbe",
        "birthday": "2020-05-26",
        "createdat": "18th March 2020",
        "description": "Premium User for 3 years",
        "email": "identify@openai.com",
        "firstname": "John",
        "userId": "sample_user_id",
        "lastname": "Sparrow",
        "name": "John Sparrow",
        "id": "sample_user_id",
        "phone": "9112340345",
        "username": "john_sparrow"
      },
      "userAgent": "Dalvik/2.1.0 (Linux; U; Android 9; AOSP on IA Emulator Build/PSR1.180720.117)"
    },
    "event": "Product Clicked",
    "integrations": {
      "All": true
    },
    "messageId": "1590431830915-73bed370-5889-436d-9a9e-0c0e0c809d06",
    "properties": {
      "revenue": "130",
      "currency": "USD",
      "quantity": "5",
      "test_key_2": {
        "test_child_key_1": "test_child_value_1"
      },
      "price": "58.0"
    },
    "originalTimestamp": "2020-05-25T18:37:10.917Z",
    "type": "track",
    "userId": "sample_user_id"
  }
]

License