css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <section class="bcls-section">
<h2 id="Request_Options">Analytics by Playlists</h2>

<p>Where no default value is indicated, there is none.</p>

<div id="inputFields" class="input-fields">
<fieldset class="bcls-fieldset"><legend>Basic Information</legend>

<p>By default, you will get results from a Brightcove sample account.</p>

<p><button class="bcls-button" id="useMyAccount">Use My Account Instead</button></p>

<div id="basicInfo" style="display:none;">
<table class="bcls-table">
	<tbody class="bcls-table__body">
		<tr>
			<td>Video Cloud Account (Publisher ID):</td>
			<td><input id="accountID" class="required aapi-request" type="text" size="55" /></td>
		</tr>
		<tr>
			<td class="align-top no-wrap">Client id:</td>
			<td><input id="client_id" class="required aapi-request" type="text" size="100" value="" /> (must include permissions for Analytics/Read, Videos/Read, and Playlists/Read)</td>
		</tr>
		<tr>
			<td class="align-top no-wrap">Client secret:</td>
			<td><input id="client_secret" class="required aapi-request" type="text" size="100" value="" /> (must include permissions for Analytics/Read, Videos/Read, and Playlists/Read)
			options
			</td>
		</tr>
	</tbody>
</table>

</div>
</fieldset>

<fieldset class="bcls-fieldset"><legend>Select Playlist</legend>

<p><button class="bcls-button" id="getPlaylists">Get Playlists</button></p>

<div class="bcls-hidden" id="playlistSelectWrapper">
<p id="playlists"><select id="playlistSelector"></select></p>
</div>
</fieldset>

<fieldset id="aapiParams" class="bcls-hidden"><legend>Filters</legend>

<div id="filters">
<table class="bcls-table">
	<tbody class="bcls-table__body">
		<tr>
			<td>From:</td>
			<td>Date: <input class="date-range" id="from" type="text" size="30" /> (default: 30 days ago -- choose the same date for From and To to get a report for that day)</td>
		</tr>
		<tr>
			<td>To:</td>
			<td>Date: <input class="date-range" id="to" type="text" size="30" /> (default: now)</td>
		</tr>
		<tr>
			<td>Where:</td>
			<td>Player ID = <input id="player" class="where-input aapi-request" type="text" size="40" value="" /></td>
		</tr>
		<tr>
			<td>Format for response data</td>
			<td><select id="format"><option selected="selected">json</option><option>csv</option></select></td>
		</tr>
	</tbody>
</table>
</div>
</fieldset>
</div>
</section>

<section class="bcls-section">
<h2 id="API_Request">API Request</h2>

<div id="requestSubmitter" class="bcls-hidden">
<fieldset class="bcls-fieldset"><legend>Generated Request</legend>

<p>API Request</p>
<textarea id="request" name="request" class="bcls-code"></textarea></fieldset>

<p><button class="bcls-button" id="submitButton">Submit Request</button></p>
</div>
</section>

<section class="bcls-section">
<h2 id="response">Response</h2>

<p>In the <strong>Summary</strong> portion of the response, you will find average values for the playlist.</p>

<pre class="line-numbers"><code class="language-json" id="responseFrame">Response will appear here...</code></pre>
</section>
            
          
!
            
              /* * The body style is just for the
 * background color of the codepen.
 * Do not include in your code.
 */
body {
  background-color: #111;
  color: #f5f5f5;
  font-family: sans-serif;
  margin: 2em;
}
/*
 * Styles essential to the sample
 * are below
 */
.bcls-code {
  color: #111;
  font-family: 'Source Code Pro',monospace;
  padding: 1em;
  width: 90%;
  min-height: 5em;
  background-color: #cfcfcf;
  font-size: 1rem;
}
.bcls-button {
  padding: .5em;
  background-color: #333;
  border: 1px yellow solid;
  border-radius: .5em;
  color: yellow;
  cursor: pointer;
}
fieldset {
  color: #64AAB2;
  border: 1px #64AAB2 solid;
  border-radius: .5em;
}
code {
  color: #E4B842;
  font-family: 'Source Code Pro',monospace;
}
            
          
!
            
              var BCLS = (function(window, document, Pikaday) {
  var getPlaylists,
    limit = 25,
    offset = 0,
    playlistData = [],
    analyticsData = {},
    useMyAccount = document.getElementById('useMyAccount'),
    basicInfo = document.getElementById('basicInfo'),
    $accountInputs = document.getElementById('accountInputs'),
    $playlistInfo = document.getElementById('playlistInfo'),
    proxyURL = 'https://solutions.brightcove.com/bcls/bcls-proxy/bcls-proxy-v2.php',
    $account_id = document.getElementById('accountID'),
    account_id = '1752604059001',
    $client_id = document.getElementById('client_id'),
    $client_secret = document.getElementById('client_secret'),
    client_id,
    client_secret,
    $APIrequestType = document.getElementById('aapiRequestType'),
    requestType,
    $accountID = document.getElementById('accountID'),
    $dimension = document.getElementById('dimension'),
    fromPicker,
    toPicker,
    to = document.getElementById('to'),
    from = document.getElementById('from'),
    $player = document.getElementById('player'),
    $requestButton = document.getElementById('requestButton'),
    $request = document.getElementById('request'),
    $requestForm = document.getElementById('requestForm'),
    $aapiParams = document.getElementById('aapiParams'),
    $requestSubmitter = document.getElementById('requestSubmitter'),
    $submitButton = document.getElementById('submitButton'),
    $format = document.getElementById('format'),
    $directVideoInput = document.getElementById('directVideoInput'),
    $responseFrame = document.getElementById('responseFrame'),
    separator = '',
    requestTrimmed = false,
    lastChar = '',
    requestURL = '',
    endDate = '',
    startDate = '',
    $getPlaylists = document.getElementById('getPlaylists'),
    $playlistSelectWrapper = document.getElementById('playlistSelectWrapper'),
    $playlistSelector = document.getElementById('playlistSelector'),
    playlistSelector = document.getElementById('playlistSelector'),
    videoIds = [],
    isJSON = true,
    searchString,
    playlistLimit,
    playlistSort,
    totalPlaylists = 0,
    totalPlaylistCalls = 0,
    callNumber = 0,
    gettingData = false,
    now = new Date(),
    nowMS = now.valueOf(),
    fromMS = nowMS - (30 * 24 * 60 * 60 * 1000),
    fromDate = new Date(fromMS),
    nowISO = now.toISOString().substr(0, 10),
    fromISO = fromDate.toISOString().substr(0, 10);
  // utilities
  // more robust test for strings 'not defined'
  function isDefined(v) {
    if (v === '' || v === null || v === 'undefined' || v === undefined) {
      return false;
    }
    return true;
  }

  /**
   * get selected value for single select element
   * @param {htmlElement} e the select element
   * @return {Object} object containing the `value` and selected `index` and 'data-playlist-index'
   */
  function getSelectedPlaylist(e) {
    var val = e.options[e.selectedIndex].value,
      playlistIndex = parseInt(e.options[e.selectedIndex].getAttribute('data-playlist-index')),
      idx = e.selectedIndex;
    return {
      value: val,
      playlistIndex: playlistIndex,
      index: idx
    };
  }

  /**
   * get selected value for single select element
   * @param {htmlElement} e the select element
   * @return {Object} object containing the `value` and selected `index`
   */
  function getSelectedValue(e) {
    var val = e.options[e.selectedIndex].value,
      idx = e.selectedIndex;
    return {
      value: val,
      index: idx
    };
  }

  /**
   * handler for playlist selection
   * get the videos from the playlist metadata or the search criteria
   * and then invokes the Analytics API request
   */
  function onPlaylistSelect() {
    var selectedPlaylistObj = getSelectedPlaylist(playlistSelector),
      selectedPlaylist = playlistData[selectedPlaylistObj.playlistIndex];

    switch (selectedPlaylist.type) {
      case 'EXPLICIT':
        videoIds = selectedPlaylist.video_ids;
        break;
      case 'ACTIVATED_OLDEST_TO_NEWEST':
        searchString = selectedPlaylist.search;
        playlistLimit = selectedPlaylist.limit;
        playlistSort = 'published_at';
        buildRequest('getVideos');
        break;
      case 'ACTIVATED_NEWEST_TO_OLDEST':
        searchString = selectedPlaylist.search;
        playlistLimit = selectedPlaylist.limit;
        playlistSort = encodeURI('-published_at');
        buildRequest('getVideos');
        break;
      case 'ALPHABETICAL':
        searchString = selectedPlaylist.search;
        playlistLimit = selectedPlaylist.limit;
        playlistSort = 'name';
        buildRequest('getVideos');
        break;
      case 'PLAYS_TOTAL':
        searchString = selectedPlaylist.search;
        playlistLimit = selectedPlaylist.limit;
        playlistSort = encodeURI('-plays_total');
        buildRequest('getVideos');
        break;
      case 'PLAYS_TRAILING_WEEK':
        searchString = selectedPlaylist.search;
        playlistLimit = selectedPlaylist.limit;
        playlistSort = 'plays_trailing_week';
        buildRequest('getVideos');
        break;
      case 'START_DATE_OLDEST_TO_NEWEST':
        searchString = selectedPlaylist.search;
        playlistLimit = selectedPlaylist.limit;
        playlistSort = 'schedule_starts_at';
        buildRequest('getVideos');
        break;
      case 'START_DATE_NEWEST_TO_OLDEST':
        searchString = selectedPlaylist.search;
        playlistLimit = selectedPlaylist.limit;
        playlistSort = encodeURI('-schedule_starts_at');
        buildRequest('getVideos');
        break;
    }
    // undim param input fields
    $aapiParams.setAttribute('style', 'opacity:1;cursor:pointer;');
    $requestSubmitter.setAttribute('style', 'opacity:1;cursor:pointer;');
    buildRequest();
  }

  /**
   * removes spaces from a String
   * @param i{String} str the string to process
   */
  function removeSpaces(str) {
    if (isDefined(str)) {
      str = str.replace(/\s+/g, '');
      return str;
    }
  }

  /**
   * builds the various API requests,
   * submits them,
   * and handles the responses
   * @param {String} type the request type
   */
  function buildRequest(type) {
    var options = {},
      tmpArray,
      newOption,
      txt,
      frag = new DocumentFragment(),
      currentIndex,
      i;

    // add credentials if submitted
    if (isDefined(client_id) && isDefined(client_secret)) {
      options.client_id = client_id;
      options.client_secret = client_secret;
    }
    options.account_id = account_id;
    options.proxyURL = proxyURL;
    switch (type) {
      case 'getPlaylistsCount':
        options.url = 'https://cms.api.brightcove.com/v1/accounts/' + account_id + '/counts/playlists';
        $request.textContent = options.url;
        makeRequest(options, function(response) {
          response = JSON.parse(response);
          totalPlaylists = response.count;
          buildRequest('getPlaylists');
        });
        break;
      case 'getPlaylists':
        totalPlaylistCalls = Math.ceil(totalPlaylists / limit);
        options.url = 'https://cms.api.brightcove.com/v1/accounts/' + account_id + '/playlists?limit=' + limit + '&offset=' + (limit * callNumber);
        $request.textContent = options.url;
        makeRequest(options, function(response) {
          response = JSON.parse(response);
          // add the current items array to overall one
          playlistData = playlistData.concat(response);
          callNumber++;
          if (callNumber < totalPlaylistCalls) {
            // still have more videos to get
            buildRequest('getPlaylists');
          } else {
            // build the playlist selector
            newOption = document.createElement('option');
            txt = document.createTextNode('Select a Playlist');
            newOption.appendChild(txt);
            frag.appendChild(newOption);
            playlistData.forEach(function(playlist, index, playlistData) {
              newOption = document.createElement('option');
              newOption.setAttribute('value', playlist.id);
              newOption.setAttribute('data-playlist-index', index);
              txt = document.createTextNode(playlist.name);
              newOption.appendChild(txt);
              frag.appendChild(newOption);
            });
            // add the options to the playlist selector
            $playlistSelector.appendChild(frag);
            // reset callNumber
            callNumber = 0;
          }
        });
        break;
      case 'getVideos':
        options.url = 'https://cms.api.brightcove.com/v1/accounts/' + account_id + 'videos?q=' + searchString + '&limit=' + playlistLimit + '&sort=' + playlistSort;
        $request.textContent = options.url;
        makeRequest(options, function(response) {
          response = JSON.parse(response);
          response.forEach(function(video, index, response) {
            videoIds.push(video.id);
          });
        });
        break;
      case 'getAnalytics':
        var formatObj = getSelectedValue($format),
          format = formatObj.value;
        if (format === 'csv') {
          isJSON = false;
        }
        options.url = 'https://analytics.api.brightcove.com/v1/data?accounts=' + account_id + '&dimensions=video&where=video==' + videoIds.join(',') + '&from=' + from.value + '&to=' + to.value + '&limit=all&fields=engagement_score,play_rate,video,video_duration,video_engagement_1,video_engagement_100,video_engagement_25,video_engagement_50,video_engagement_75,video_impression,video_name,video_percent_viewed,video_seconds_viewed,video_view' + '&format=' + format;
        $request.textContent = options.url;
        makeRequest(options, function(response) {
          response = JSON.parse(response);
          if (format === 'json') {
            $responseFrame.textContent = JSON.stringify(response, null, '  ');
          } else {
            $responseFrame.textContent = response;
          }
        });
        break;
    }
  }

  /**
   * send API request to the proxy
   * @param  {Object} options for the request
   * @param  {String} options.url the full API request URL
   * @param  {String="GET","POST","PATCH","PUT","DELETE"} requestData [options.requestType="GET"] HTTP type for the request
   * @param  {String} options.proxyURL proxyURL to send the request to
   * @param  {String} options.client_id client id for the account (default is in the proxy)
   * @param  {String} options.client_secret client secret for the account (default is in the proxy)
   * @param  {JSON} [options.requestBody] Data to be sent in the request body in the form of a JSON string
   * @param  {Function} [callback] callback function that will process the response
   */
  function makeRequest(options, callback) {
    var httpRequest = new XMLHttpRequest(),
      response,
      proxyURL = options.proxyURL,
      // response handler
      getResponse = function() {
        try {
          if (httpRequest.readyState === 4) {
            if (httpRequest.status >= 200 && httpRequest.status < 300) {
              response = httpRequest.responseText;
              // some API requests return '{null}' for empty responses - breaks JSON.parse
              if (response === '{null}') {
                response = null;
              }
              // return the response
              callback(response);
            } else {
              alert('There was a problem with the request. Request returned ' + httpRequest.status);
            }
          }
        } catch (e) {
          alert('Caught Exception: ' + e);
        }
      };
    /**
     * set up request data
     * the proxy used here takes the following request body:
     * JSON.stringify(options)
     */
    // set response handler
    httpRequest.onreadystatechange = getResponse;
    // open the request
    httpRequest.open('POST', proxyURL);
    // set headers if there is a set header line, remove it
    // open and send request
    httpRequest.send(JSON.stringify(options));
  }

  // add date pickers to the date input fields
  fromPicker = new Pikaday({
    field: document.getElementById('from'),
    format: 'YYYY-MM-DD',
    onSelect: buildRequest
  });
  toPicker = new Pikaday({
    field: document.getElementById('to'),
    format: 'YYYY-MM-DD',
    onSelect: buildRequest
  });
  // populate to/from fields with default values
  to.value = nowISO;
  from.value = fromISO;

  // set event listeners
  useMyAccount.addEventListener('click', function() {
    if (basicInfo.getAttribute('style') === 'display:none;') {
      basicInfo.setAttribute('style', 'display:block;');
      useMyAccount.textContent = 'Use Sample Account';
    } else {
      basicInfo.setAttribute('style', 'display:none;');
      useMyAccount.textContent = 'Use My Account Instead';
    }
  });

  // event listeners
  $getPlaylists.addEventListener('click', function() {
    if (isDefined($account_id.value)) {
      account_id = $account_id.value;
      if (isDefined($client_id.value) && isDefined($client_secret.value)) {
        client_id = $client_id.value;
        client_secret = $client_secret.value;
      } else {
        alert('You must provide a client id and secret');
      }
    }
    buildRequest('getPlaylists');
  });

  playlistSelector.addEventListener('change', onPlaylistSelect);

  // send request
  $submitButton.addEventListener('click', function() {
    buildRequest('getAnalytics');
  });

  return {
    onPlaylistSelect: onPlaylistSelect
  };
})(window, document, Pikaday);

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console