Wednesday, June 19, 2024
- Advertisement -
More

    Latest Posts

    Set Language Of Person Submitting Your Realtime Marketing Form

    Using Realtime Marketing from Dynamics 365 (Customer Insights – Journeys) gives you the ability to customise your emails, and using dynamic conditional content, set different variations based on field values. One common field to use is that of a custom language field. You could ask for this information, or you could use a piece of information found within the developer console of a browser that shows the language set for the person filling out a form. In this post, I will walk through a way you can use that information to set a language field for each Lead or Contact filling out your Realtime forms.

    First, determine which languages you would like to use. There are a couple of language fields you might already see (for example, if you have a portal installed), but I would suggest adding a new global Choice field for this and call it something like Preferred Language, or Marketing Language. Add in all of the languages you want to offer content in and include them in the field. Make a note of the value numbers that are assigned. That will be needed later on.

    Set Language Of Person Submitting Your Realtime Marketing Form
    Click to view in detail

    Now add a new field to the Lead and/or Contact table called Language (or whatever you want to call it). For this, choose the option to sync with a global choice, and then pick the new Choice field you added above.

    Click to view in detail

    If you do this for both Lead and Contact, best practice would be to add to the Lead to Contact mapping and make sure your new language fields are mapped. That way, when qualifying a Lead, the Language will be mapped through to the new Contact record created so you won’t miss that data moving forwards.

    Click to view in detail

    Now add your new field to your Realtime Marketing form(s). For this, I have a form that will create and update Leads. Once the field has been added, I’ve marked the field as hidden so it’s not shown to anyone.

    Click to view in detail

    Now comes the script… but a couple of things to be aware of first. When picking a language for the browser, if I look for English, you can see there are many options. Each one has a different code, but all would be prefixed with en for English. Canada would be en-CA, India is en-IN, and United Kingdom is en-GB.

    Click to view in detail

    You can find all of the two digit codes here, so make sure you review for the language list you have created on your choice field, as those will be needed for the script. DO NOT try and add all variations of a language like I mentioned above. You only need to map the base language. https://www.w3schools.com/tags/ref_language_codes.asp

    Moving on to the script, you will need to add it directly below the closing </style> tag like you see below, and before the closing </head> tag.

    Click to view in detail

    I’ll walk through the script now to explain. The first thing it does is wait until the form loads on the page before running through and trying to do anything. Then it does the following:

    • Sets the language of the user variable by accessing navigator.language from the browser console
    • Sets the language field you created by accessing it via the ‘title’ of the field. If you didn’t name the field on the Lead and/or Contact Language, then you will need to adjust that to include the field name you DID use
    • Next we create some language mappings. This is where you will need to have the two character code from the list of languages provided on the link above, and then map to the value for the same language in your new global choice field. For example, fr is for French which maps to a value of 916780001 on my choice field
    • After setting all of the mappings, the script is setting a default language (English in my example) so that if the browser language doesn’t match any listed in the mappings, something will still be set
    • Then the list of mapped languages is checked against the langUser variable to see if there is a match. It uses ‘contains’ rather than equals so that it doesn’t matter what variant of a language someone uses in their browser, a match can still be made
    • The found language code is then set using the matching value on to the Language choice field on the form
    • Finally, the Langauge and Value are being logged in the Developer Console so you will know it’s working (not required, but handy to have)
    <script>
        document.addEventListener('d365mkt-afterformload', function () {
            var langUser = navigator.language;
            var langField = document.querySelector("select[title='Language']");
    
            if (langField) {
                var languageMappings = {
                    'fr': '916780001',
                    'de': '916780002',
                    'es': '916780003',
                    'nl': '916780004',
                    'it': '916780005',
                    'sv': '916780006'
                };
                var defaultLanguage = '916780000';
                var selectedValue = defaultLanguage;
    
                // Check if langUser contains any language code
                for (var languageCode in languageMappings) {
                    if (langUser.toLowerCase().includes(languageCode)) {
                        selectedValue = languageMappings[languageCode];
                        break;
                    }
                }
                // Clear all selections first
                for (var i = 0; i < langField.options.length; i++) {
                    langField.options[i].selected = false;
                }
                // Set the selected option
                var selectedOption = langField.querySelector("option[value='" + selectedValue + "']");
                if (selectedOption) {
                    selectedOption.selected = true;
                } else {
                    console.error("Selected option not found: " + selectedValue);
                }
                console.log("Language: " + langUser, "Language Value: " + langField.value);
            } else {
                console.error("Language field not found");
            }
        });
    </script>

    Once you have updated the script above to meet your own requirements, add it to your Realtime Marketing form and make it live. Then go and check out your form. Oh, one little trick to make sure you are looking at the most up to date version of your form, add the following to the end of the URL where your form is embedded #d365mkt-nocache. That will make sure you see it exactly as it should be.

    Now press function and F12 on the webpage. It should open up the Developer Console for you to review. If it all worked, you should see that the language and language value are showing in the log. Here we can see en-GB for my browser which is the language I use. The language of the form is also set to English.

    Click to view in detail

    If I update the language in the browser settings to German (Switzerland), we can see the language passed through is de-CH, which I have mapped to make sure the language on the new Lead or Contact record is set to German.

    Click to view in detail

    Once the form is submitted, the language field will be set on the submission. You’ll be able to see the label and value from the option that was set on the field submission for the Language field and it will be set accordingly on the related Lead or Contact record.

    Click to view in detail

    Last thing, if you want to use this in your emails, here is how you can do that. First, from the section you’ve added your text box to, click on the enable conditional content option.

    Click to view in detail

    Then you can define a condition using the language field from the record and indicate the condition to determine when this variation of the section will be shown. So here, the condition being looked for is that the language on the Lead record equals French.

    Click to view in detail

    If it does, the French version will be displayed. Simple!

    Click to view in detail

    Latest Posts

    - Advertisement -

    Don't Miss

    Stay in touch

    To be updated with all the latest news, offers and special announcements.