Journey API
Marketing Cloud

Automate Real-Time Emails on Sign-Up with Journey Builder

Use Case:

A client wants to trigger a journey event when a user submits a sign up form on a Cloud Page and create a contact in the CRM. After contact creation, this journey event will add the user to a specific journey in Marketing Cloud, where they will receive a series of personalized emails.

Prerequisite:

  • Should have Salesforce CRM Enterprise or above edition.
  • Salesforce CRM should be integrated to SFMC using Marketing Cloud Connect.

Solution:

In this solution, we use SSJS and AmpScript functions to insert the Salesforce CRM contact records. There is no HTML and CSS on this solution overview so you can add yours as needed. The following approach is used in the solution:

  • Design a form on the Cloud Page to collect user information (e.g., name, email).
  • Use AMPscript or SSJS (Server-Side JavaScript) to handle the form submission.
  • Use the collected data to make a POST request to the Journey Event API.

Steps:

  1. Create a Data Extension with the Sign Up page form input fields.

    MarketingCloud

  2. Create a Journey:
    1. Select entry source API Event
    2. Create new API Event and Select above created Data Extension
    3. Select an email message in the email activity.
    4. Save and validate the journey then publish it.

    MarketingCloud

  3. Create a landing CloudPage with a Sign Up Form and Server-Side Script to Call the Journey Event API on form submit.

    MarketingCloud

Example Code:

Here is an sample code using SSJS and Ampscript after form submission creating a contact in the Salesforce CRM and triggering the Sign Up Journey.


<!--Using Ampscript to create a Contact record in the CRM -->
%%[ 

Var @fname, @lname, @email, @createContact,@conId 

If RequestParameter("submit") == "Submit" then 

/******Store input values in the Ampscript variables*******/ 

SET @fname = RequestParameter("firstname") 

SET @lname = RequestParameter("lastname") 

SET @email = RequestParameter("email") 

/****Check inputs are not blank on submit and create a contact in the CRM******/ 

If Not Empty(@fname) AND Not Empty(@lname) AND Not Empty(@email) then 

SET @createContact =  

 CreateSalesforceObject("Contact",3,"First_Name__c", @fname,"Last_Name__c", @lname, "Email__c",@email)  

ELSE 

Output(Concat("Error: Please fill all the required fields")) 

EndIf 

/******Store contactID after contact creation used to store unique contact in the DE**********/ 

If @createContact > 0 then 

SET @conId = @createContact 

ENDIF 

EndIf 

]%% 

<!--Using SSJS to call the Journey Event API: -->

<script runat = server language="JavaScript"> 

 /******Load Platform Library******/ 

  Platform.Load("Core", "1"); 

   var response = { 

    "result": "", 

    "message": "" 

  } 

  try{ 

 /******Get User data on Submit******/ 

    var conId= Variable.GetValue("@conId"); 

    var firstName = Variable.GetValue("@fname"); 

    var lastName = Variable.GetValue("@lname"); 

    var emailAdd = Variable.GetValue("@email"); 

    var clientSecret = "Your_Client_Secret"; 

    var clientId = "Your_Client_ID"; 

    var mid = "Your_AccountID"; 

 /******Checking Access token and post data on Journey Event REST API******/ 

    if(emailAdd != undefined && emailAdd != null && emailAdd != ""){ 

      var url = 'YOUR_AUTH_SUBDOMAIN /v2/token'; 

      var contentType = 'application/json'; 

      var payload = Stringify({ 

        "grant_type": "client_credentials", 

        "client_id": clientId, 

        "client_secret": clientSecret, 

        "account_id": mid 

      } 

                             ); 

      var headerNames = ["Encoding"]; 

      var headerValues = ["utf-8"]; 

      var tokenResponse; 

      var accessToken; 

      var rest_instance_url; 

      var accessTokenRequest = HTTP.Post(url, contentType, payload, headerNames, headerValues); 

         if(accessTokenRequest.StatusCode == 200){ 

        tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]); 

        accessToken = tokenResponse.access_token; 

        rest_instance_url = tokenResponse.rest_instance_url; 

 /******Checking Access token and post data on Journey Event REST API******/ 

        if(accessToken != null){ 

          var requestUrl = rest_instance_url + "interaction/v1/events"; 

          var contentType = 'application/json'; 

          var headerNames = ["Authorization"]; 

          var headerValues = ["Bearer "+accessToken]; 

          var jsonBody = Stringify({ 

            "ContactKey": conId, 

            "EventDefinitionKey": "YOUR_EVENT_DEFINITION_KEY", 

            "Data": { 

     "ContactId": conId, 

              "Firstname": firstName, 

              "Lastname": lastName, 

              "Email": emailAdd 

            } 

          }); 

           try{ 

            var result = HTTP.Post(requestUrl, contentType, jsonBody, headerNames, headerValues); 

            response.result = "debug message"; 

            response.message = Stringify(result); 

            Write(Stringify(response)); 

            if(result.StatusCode == 200 || result.StatusCode == 201){ 

              response.result = "success"; 

              response.message = "API journey call"; 

              Write(Stringify(response)); 

            } 

          } 

          catch(er){ 

            response.result = "Journey error"; 

            response.message = Stringify(er); 

            Write(Stringify(response)); 

          }  

        }  

      } 

    } 

  } 

  catch(err){ 

    response.result = "Data error"; 

    response.message = Stringify(err); 

    Write(Stringify(response)); 

  } 

</script> 

Notes:

  • Replace Contact object API field names to create a contact.
  • Replace YOUR_EVENT_DEFINITION_KEY with the actual event definition key from your journey.
  • Replace YOUR_AUTH_SUBDOMAIN with your Marketing Cloud subdomain.
  • Replace Your_Client_Secret, Your_Client_ID, and Your_AccountID with your Marketing Cloud server-to-server integration package details.
  • This code can be modified as per the requirement.

Conclusion:

Automatically send personalized emails to new users right after they sign up. This instant engagement provides a warm welcome and necessary information, enhancing user interaction with timely and personalized communication.

Hope this helps, please do share and comment.

Leave a Reply