Sample Code

    Sample Code


    Article summary

    Shoplive Player sample code

    Using the messageCallback function redefines the execution behavior when coupon download, product click, or login-required function access events occur. Then display the Shoplive Player with a style that applies to both desktop and mobile sizes.

    <html>
      <head>
        <title>Shoplive - Simple player example</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, viewport-fit=cover, shrink-to-fit=no" />
        <script type="text/javascript" src="https://static.shoplive.cloud/shoplive.js"></script>
        <script>
          let messageCallback = {
            DOWNLOAD_COUPON: function (payload) {
              // get Current Player
              var player = cloud.shoplive.getPlayer();
              // if, ONAIR player
              if (
                player.campaignStatus === "ONAIR" &&
                player.campaignKey === payload.campaignKey
              ) {
                // copy to clipboard
                var textarea = document.createElement("textarea");
                document.body.appendChild(textarea);
                textarea.value = payload.coupon;
                textarea.select();
                document.execCommand("copy");
                document.body.removeChild(textarea);
                // Show Dialog
                player.sendMessage("showDialog", {
                  title: "Copy to clipboard completed.",
                  message: payload.coupon,
                });
              }
            },
            CLICK_PRODUCT: function (payload) {
              // You can add a new parameter to the URL of the clicked product.
              var url = payload.url + "&parameter=" +"{your_parameter}";
    
              // The product page will be opened in the current window.
              location.href = url;
            },
            LOGIN_REQUIRED: function (payload) {
              alert("LOGIN_REQUIRED");
              location.href = "YOUR_LOGIN_PAGE_URL";
            },
          };
          cloud.shoplive.init({
            accessKey: "{YOUR_ACCESS_KEY}",
            messageCallback: messageCallback,
          });
          
          let userId = "USER_ID";
          let userName = "USER_NAME";
          if (userId) {
            cloud.shoplive.setUserObject({ userId: userId, userName: userName });
          }
        </script>
        <style>
          html,
          body {
            margin: 0;
            padding: 0;
            background-color: #333;
          }
          #shoplive-player {
            max-width: calc(100vh / 16 * 9 - 2px);
            margin: auto;
          }
          @media screen and (max-width: 480px) {
            #shoplive-player {
              max-width: 100%;
            }
          }
        </style>
      </head>
      <body>
        <div id="shoplive-player"></div>
        <script defer>
          cloud.shoplive.setPlayer("shoplive-player", { campaignKey: "YOUR_CAMPAIGN_KEY" });
        </script>
      </body>
    </html>


    What's Next