diff --git a/website/api/controllers/create-or-update-one-newsletter-subscription.js b/website/api/controllers/create-or-update-one-newsletter-subscription.js index 6585257b36..47081fb86f 100644 --- a/website/api/controllers/create-or-update-one-newsletter-subscription.js +++ b/website/api/controllers/create-or-update-one-newsletter-subscription.js @@ -41,20 +41,28 @@ module.exports = { .tolerate('E_UNIQUE'); await NewsletterSubscription.updateOne({emailAddress: emailAddress}).set({isSubscribedToReleases: true}); - + let psychologicalStageChangeReason = 'Website - Newsletter'; + 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: emailAddress, contactSource: 'Website - Newsletter', description: `Subscribed to the Fleet newsletter`, psychologicalStage: '3 - Intrigued', - psychologicalStageChangeReason: 'Website - Newsletter', + psychologicalStageChangeReason, + intentSignal: 'Subscribed to the Fleet newsletter', }).exec((err)=>{// Use .exec() to run the salesforce helpers in the background. if(err) { sails.log.warn(`Background task failed: When a user signed up for a newsletter, a lead/contact could not be updated in the CRM for this email address: ${emailAddress}.`, err); } return; - }); + });//_∏_ // All done. return; 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 0a820c109a..7889d9e03b 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 @@ -44,6 +44,15 @@ module.exports = { }, getStartedResponses: { type: 'string', + }, + intentSignal: { + type: 'string', + isIn: [ + 'Subscribed to the Fleet newsletter', + // 'Signed up for a fleetdm.com account',// + // 'Submitted the "Talk to us" form', + // 'Submitted the "Send a message" form', + ], } }, @@ -60,8 +69,7 @@ module.exports = { }, - - fn: async function ({emailAddress, linkedinUrl, firstName, lastName, organization, primaryBuyingSituation, psychologicalStage, psychologicalStageChangeReason, contactSource, description, getStartedResponses}) { + fn: async function ({emailAddress, linkedinUrl, firstName, lastName, organization, primaryBuyingSituation, psychologicalStage, psychologicalStageChangeReason, contactSource, description, getStartedResponses, intentSignal}) { // Return undefined if we're not running in a production environment. if(sails.config.environment !== 'production') { sails.log.verbose('Skipping Salesforce integration...'); @@ -114,6 +122,9 @@ module.exports = { if(psychologicalStageChangeReason) { valuesToSet.Psystage_change_reason__c = psychologicalStageChangeReason;// eslint-disable-line camelcase } + if(intentSignal) { + valuesToSet.Intent_signals__c = intentSignal;// eslint-disable-line camelcase + } let existingContactRecord; // Search for an existing Contact record using the provided email address or linkedIn profile URL. @@ -134,6 +145,21 @@ module.exports = { if(description && existingContactRecord.Description) { valuesToSet.Description = existingContactRecord.Description + '\n' + description; } + // 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) { + // Convert the string from the Salesforce record into an array. + let existingContactIntentSignalsAsAnArray = existingContactRecord.Intent_signals__c.split(';'); + // If this intent signal is not included in the exisitng contacts intent signals, add it. + if(!existingContactIntentSignalsAsAnArray.includes(intentSignal)) { + existingContactIntentSignalsAsAnArray.push(intentSignal); + // Convert the array back into a string to send it to Salesforce. + valuesToSet.Intent_signals__c = existingContactIntentSignalsAsAnArray.join(';');// eslint-disable-line camelcase + } else { + // Otherwise, if the existing contact already has this intent signal tracked, remove it from the valuesToSet + delete valuesToSet.Intent_signals__c; + } + } // Check the existing contact record's psychologicalStage. if(psychologicalStage) { let recordsCurrentPsyStage = existingContactRecord.Stage__c;