DEV Community

Tiktkviewer
Tiktkviewer

Posted on

Parsing Abbreviated View Counts and Computing Engagement Rates in JavaScript

Short-video platforms show view, like and comment counts in abbreviated form: 1.2K, 3.4M, 987. If you scrape public pages for research or build a dashboard, you will need to turn those strings back into numbers, and then into something meaningful like an engagement rate. This post covers both in plain JavaScript, with the edge cases that tend to break naive code.

Parsing abbreviated counts

const MULTIPLIERS = { K: 1e3, M: 1e6, B: 1e9 };

function parseCount(text) {
  if (text == null) return null;
  const s = String(text).trim().toUpperCase().replace(/,/g, "");
  const m = s.match(/^(\d+(?:\.\d+)?)\s*([KMB])?$/);
  if (!m) return null;
  const n = parseFloat(m[1]) * (m[2] ? MULTIPLIERS[m[2]] : 1);
  return Math.round(n);
}

parseCount("1.2K");   // 1200
parseCount("3.4M");   // 3400000
parseCount("987");    // 987
parseCount("12,345"); // 12345
Enter fullscreen mode Exit fullscreen mode

Things to watch:

  • Locale separators. Some locales display 1,2K (comma as decimal). If you collect data from several regions, normalise per locale before parsing, or the comma removal above will turn 1,2K into 12K.
  • Rounding. 1.2K might be anything from 1,150 to 1,249. Treat abbreviated numbers as estimates, and never compare two K values to the unit.
  • Non-numeric placeholders such as -- or an empty string should return null, not 0.

Engagement rate per video

The most common definition is interactions divided by views:

function engagementRate({ views, likes, comments, shares = 0 }) {
  if (!views) return null;
  return (likes + comments + shares) / views;
}

const v = { views: parseCount("48.3K"), likes: parseCount("5.1K"), comments: parseCount("212") };
console.log((engagementRate(v) * 100).toFixed(1) + "%"); // about 11.0%
Enter fullscreen mode Exit fullscreen mode

For accounts, average the per-video rates over the last 10 to 20 videos rather than dividing totals; one viral video otherwise dominates the result.

Median, not mean

Views on short-video platforms are extremely skewed. A creator with nine videos at 2K views and one at 900K has a mean of about 92K and a median of 2K. The median tells you what a typical post does:

function median(nums) {
  const a = nums.filter((n) => n != null).sort((x, y) => x - y);
  if (!a.length) return null;
  const mid = Math.floor(a.length / 2);
  return a.length % 2 ? a[mid] : (a[mid - 1] + a[mid]) / 2;
}
Enter fullscreen mode Exit fullscreen mode

Putting it together

function summarise(videos) {
  const rates = videos.map(engagementRate).filter((r) => r != null);
  return {
    medianViews: median(videos.map((v) => v.views)),
    medianEngagement: median(rates),
    posts: videos.length,
  };
}
Enter fullscreen mode Exit fullscreen mode

A summary like this, median views plus median engagement, is far more useful for comparing creators than follower counts.

When you do not want to write a scraper

For a quick look at a public profile's numbers without building anything, a web-based TikTok profile stats viewer shows the figures in the browser; the functions above are handy when you copy those numbers into your own spreadsheet or tool.

Keep in mind that only public data should be collected, and platform terms on automated access apply to any scraper you write.

Top comments (0)