Custom widget templates with HTML, CSS and placeholders
Write a traffic.cx widget in your own HTML and CSS with placeholders for live visitors, totals, lists and the ticker, and save it for every site in your team.
Code mode lets you write a widget in your own HTML and CSS. You put placeholders such as {{visitors}} where the numbers go, and traffic.cx fills them in, every time the widget draws. Code mode and saved templates are on paid plans. For the basics of making and embedding a widget, start with Embed a widget.
Switch to code mode
- Open a widget in the builder and choose the Code tab.
- Turn on code mode. The boxes start with a small example that shows a live dot, the live count, visitors for the period and a chart.
- Edit the HTML and CSS. The preview redraws as you type, and the builder lists any unknown placeholder or unclosed list beside the box, with a suggestion when you've made a typo.
- Choose Save.
The widget's period and number style still come from the Numbers tab: choose there which period {{visitors}} covers and whether it reads 48.2k or 48,210.
Placeholders
Each placeholder becomes one value, as text. Values are escaped before they go in, so a page title can never add markup to your widget.
| Placeholder | Becomes |
|---|---|
{{live}} | People on the site now (a pageview in the last 5 minutes) |
{{visitors}} | Unique visitors in the period |
{{visits}} | Visits in the period |
{{pageviews}} | Pageviews in the period |
{{revenue}} | Revenue in the period, in your site's currency. Needs Share revenue on |
{{change.visitors}} | The change against the previous period of the same length, such as +12%. Also {{change.visits}}, {{change.pageviews}} and {{change.revenue}} |
{{period}} | The period's name: "today", "last 30 days", "this month"… |
{{site}} | The site's name |
{{milestone}} | The last round number of visitors passed, such as 25,000 |
Lists
A list repeats the HTML between its opening and closing tags once per row, up to five rows:
html
<ul>
{{#each top_pages}}
<li>{{name}}: {{visitors}}</li>
{{/each}}
</ul>| List | Names you can use inside |
|---|---|
top_pages | name (the page's path), visitors |
top_countries | name (the country, with its flag), visitors |
top_sources | name (the source, such as Google), visitors |
ticker | country, page, source, ago ("just now", "2 min ago") |
The ticker follows the same privacy rules as the ticker template: countries only, pages and sources only when you switch them on in Numbers, and nothing while fewer than 3 people are on the site. See what a widget shares.
traffic.cx elements
Three elements draw things you can't write in plain HTML:
| Element | Draws |
|---|---|
<tcx-live-dot></tcx-live-dot> | The breathing green live dot |
<tcx-sparkline></tcx-sparkline> | A small line chart of daily visitors for the period |
<tcx-branding></tcx-branding> | The traffic.cx link, where you place it. On the Free plan it's added at the end if you leave it out; on paid plans you can switch it off |
What HTML and CSS are allowed
Your widget draws inside its own sealed box (a Shadow DOM), so your CSS can't change the rest of the page, and the page's CSS can't break your widget. What you write is cleaned when you save and again every time it draws.
- Allowed tags:
div,span,p,a,strong,em,b,i,small,br,hr,h1toh6,ul,ol,li,figure,figcaption,section,header,footer,img, simple SVG (svg,g,path,circle,rect,line,polyline), and the three traffic.cx elements. - Allowed attributes:
class,id,title,alt,role,aria-*,width,heightand SVG drawing attributes. Links must usehttps:ormailto:, and imageshttps:. - Removed: scripts,
<style>inside the HTML, iframes, forms and inputs,on…event attributes,javascript:anddata:links, and thestyleattribute. Put your styles in the CSS box. - CSS: no
@import, nourl()excepthttps:, and noexpression().
JavaScript never runs in a widget, whatever you write.
Save it as a template
Choose Save as a template in the builder to keep a design in your team's library, up to 20. It then appears at the top of the gallery, for every site in the team.
A saved template is a copy: editing it later doesn't change widgets already made from it, and changing a widget doesn't change the template. Delete a template from the gallery when you no longer need it.
On the Free plan
If a paid team moves to Free, its code-mode widgets stay up: they fall back to the look of the template they started from, and the traffic.cx link comes back. Upgrade again and your HTML and CSS return as you left them.