From 2d8ebb4388e6edd966e921e2d69626da40c76b2b Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 11 Nov 2024 12:35:49 -0600 Subject: [PATCH] Website: Update CRM helper and usage (#23670) Changes: - Updated the update-or-create-contact-and-account helper to update created contact records with a psychological stage - Updated places where we were using the update-or-create-contact-and-account helper to set a contact source on new records. --- .../save-billing-info-and-subscribe.js | 1 + website/api/controllers/entrance/signup.js | 12 +++++++-- .../update-or-create-contact-and-account.js | 25 +++++++++++++------ website/api/hooks/custom/index.js | 1 + 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/website/api/controllers/customers/save-billing-info-and-subscribe.js b/website/api/controllers/customers/save-billing-info-and-subscribe.js index 6369ebb06c..4607dfa71e 100644 --- a/website/api/controllers/customers/save-billing-info-and-subscribe.js +++ b/website/api/controllers/customers/save-billing-info-and-subscribe.js @@ -169,6 +169,7 @@ module.exports = { firstName: this.req.me.firstName, lastName: this.req.me.lastName, organization: this.req.me.organization, + contactSource: 'Website - Sign up',// Note: this is only set on new contacts. description: `Purchased a self-service Fleet Premium license on ${isoTimestampForDescription.split('T')[0]} for ${quoteRecord.numberOfHosts} host${quoteRecord.numberOfHosts > 1 ? 's' : ''}.` }).exec((err)=>{ if(err){ diff --git a/website/api/controllers/entrance/signup.js b/website/api/controllers/entrance/signup.js index c9129ac3c6..4ba8d918db 100644 --- a/website/api/controllers/entrance/signup.js +++ b/website/api/controllers/entrance/signup.js @@ -138,13 +138,21 @@ the account verification message.)`, .intercept({name: 'UsageError'}, 'invalid') .fetch(); - + let psychologicalStageChangeReason; + if(this.req.session.adAttributionString && this.req.session.visitedSiteFromAdAt) { + let sevenDaysAgoAt = Date.now() - (1000 * 60 * 60 * 24 * 7); + // If this user visited the website from an ad, set the psychologicalStageChangeReason to be the adCampaignId stored in their session. + if(this.req.session.visitedSiteFromAdAt > sevenDaysAgoAt) { + psychologicalStageChangeReason = this.req.session.adAttributionString; + } + } sails.helpers.salesforce.updateOrCreateContactAndAccount.with({ emailAddress: newEmailAddress, firstName: firstName, lastName: lastName, organization: organization, - contactSource: 'Website - Sign up' + contactSource: 'Website - Sign up', + psychologicalStageChangeReason, }).exec((err)=>{ if(err){ sails.log.warn(`Background task failed: When a user (email: ${newEmailAddress} signed up for a fleetdm.com account, a Contact and Account record could not be created/updated in the CRM.`, err); diff --git a/website/api/helpers/salesforce/update-or-create-contact-and-account.js b/website/api/helpers/salesforce/update-or-create-contact-and-account.js index 7889d9e03b..0f2c26c965 100644 --- a/website/api/helpers/salesforce/update-or-create-contact-and-account.js +++ b/website/api/helpers/salesforce/update-or-create-contact-and-account.js @@ -110,18 +110,12 @@ module.exports = { if(primaryBuyingSituation) { valuesToSet.Primary_buying_situation__c = primaryBuyingSituation;// eslint-disable-line camelcase } - if(psychologicalStage) { - valuesToSet.Stage__c = psychologicalStage;// eslint-disable-line camelcase - } if(getStartedResponses) { valuesToSet.Website_questionnaire_answers__c = getStartedResponses;// eslint-disable-line camelcase } if(description) { valuesToSet.Description = description; } - if(psychologicalStageChangeReason) { - valuesToSet.Psystage_change_reason__c = psychologicalStageChangeReason;// eslint-disable-line camelcase - } if(intentSignal) { valuesToSet.Intent_signals__c = intentSignal;// eslint-disable-line camelcase } @@ -145,6 +139,13 @@ module.exports = { if(description && existingContactRecord.Description) { valuesToSet.Description = existingContactRecord.Description + '\n' + description; } + // If we're updating a contact, add psychologicalStage and psychologicalStageChangeReason to the dictionary of valuesToSet. + if(psychologicalStage) { + valuesToSet.Stage__c = psychologicalStage;// eslint-disable-line camelcase + } + if(psychologicalStageChangeReason) { + valuesToSet.Psystage_change_reason__c = psychologicalStageChangeReason;// eslint-disable-line camelcase + } // If an intent signal was specified, add it to the list of intent signals on the exisitng contact. // Note: intent signals values are stored as a single string in salesforce, separated by a semicolon. if(intentSignal && existingContactRecord.Intent_signals__c) { @@ -260,6 +261,7 @@ module.exports = { } // console.log(`creating new Contact record.`) // Create a new Contact record for this person. + let newContactRecord = await salesforceConnection.sobject('Contact') .create({ AccountId: salesforceAccountId, @@ -268,8 +270,17 @@ module.exports = { LastName: lastName ? lastName : '?', ...valuesToSet, }); - // console.log(`Created ${newContactRecord.id}`); salesforceContactId = newContactRecord.id; + + // Since we've created a new contact, we'll update the psychological stage to be either '2 - Aware', or whatever psystage was provided. + // This causes it to appear as an edit in our CRM and helps reporting. + await salesforceConnection.sobject('Contact') + .update({ + Id: salesforceContactId, + Stage__c: psychologicalStage ? psychologicalStage : '2 - Aware',// eslint-disable-line camelcase + Psystage_change_reason__c: psychologicalStageChangeReason ? psychologicalStageChangeReason : null,// eslint-disable-line camelcase + }); + // console.log(`Created ${newContactRecord.id}`); }//fi return { diff --git a/website/api/hooks/custom/index.js b/website/api/hooks/custom/index.js index c80fc58071..0c421415d2 100644 --- a/website/api/hooks/custom/index.js +++ b/website/api/hooks/custom/index.js @@ -302,6 +302,7 @@ will be disabled and/or hidden in the UI. firstName: sanitizedUser.firstName, lastName: sanitizedUser.lastName, organization: sanitizedUser.organization, + contactSource: 'Website - Sign up',// Note: this is only set on new contacts. }); let jsforce = require('jsforce'); // login to Salesforce