1

I am trying to implement the functionality of rendering different image based on the device's OS it is getting rendered on for eg: iOS, Android. I have written the below SSJS code in my Freeform block within Marketing Cloud and I am trying to print the Output of variable 'OS' in my code, but the output is always Unknown irrespective of the email being rendered in Android/iOS.

Here is the Code :

    <script runat="server">
  Platform.Load('Core','1.1.1');
  var OS;
  
  function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

      // Windows Phone must come first because its UA also contains "Android"
    if (/windows phone/i.test(userAgent)) {
        return "Windows Phone";
    }

    if (/android/i.test(userAgent)) {
        return "Android";
    }

    // iOS detection
    if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
        return "iOS";
    }

    return "unknown";
}
var OS = getMobileOperatingSystem();
  
   Variable.SetValue('@OSType',OS);
  
</script>
0

You're using browser/client objects inside a server code. navigator and window are unavailable on server side.

To get UserAgent in SSJS retrieve it directly from the header with GetRequestHeader():

var userAgent = Platform.Request.GetRequestHeader('User-Agent');
1
  • I tried and its the same output "unknown" for all scenarios. I think what I am trying to achieve is not possible through SSJS. – Sourab Mund Jul 17 '20 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.