Hold iframes, images, and other page elements until the visitor accepts

Rename an element in your page to its TermsFeed Privacy Consent replaceable tag, add the vendor ID, and turn on the vendor switch, in 5 steps and about 10 minutes. A replaceable tag is a stand-in element that loads nothing and stays hidden. Once the visitor accepts the vendor, Privacy Consent creates the real element next to it, with every attribute you gave the replaceable tag.

Have access to the page markup that holds the element, such as a template or an HTML block, and a vendor for the service. Create the vendor with Add a Vendor From a Template, or with Add a Custom Vendor for a service with no template.

The 5 elements that have a replaceable tag

Each element has its own replaceable tag. Only iframes and images show the embed placeholder, and the real element always appears directly after the replaceable tag.

Element Replaceable tag Before consent
<iframe> <termsfeed-privacy-consent-tag-iframe> The embed placeholder
<img> <termsfeed-privacy-consent-tag-img> The embed placeholder
<script> <termsfeed-privacy-consent-tag-script> Nothing
<style> <termsfeed-privacy-consent-tag-style> Nothing
<link> <termsfeed-privacy-consent-tag-link> Nothing

Step 1: Turn on the vendor switch

A vendor from an embed template (YouTube, Vimeo, Google Maps, Spotify, SoundCloud) has this on already, so go to step 2. For a custom vendor, open "Vendors" in the TermsFeed app, click "Edit", open the "Replaceable tags" tab, turn on "Has replaceable tags?", and click "Save vendor".

Without the switch, a replaceable tag of the vendor does not get an embed placeholder and never loads. Turn it on when you create the vendor, because this edit on an existing vendor asks visitors who answered earlier to answer again. Some accounts show the tab locked, so read where replaceable tags are available in Replaceable tags.

Step 2: Copy the vendor ID

On the "Vendors" page, click "Important tags" on the row of the vendor. The row opens with one tab per element (iframe, img, script, style, link), each showing "Your code" and then "Replace with". The replaceable tag under "Replace with" holds the vendor ID in data-termsfeed-pc1-vendor-id.

A vendor ID is termsfeed- followed by 13 characters, such as termsfeed-1a2b3c4d-5e6f. Copy all of it, because an ID with a changed character does not match the vendor and the element never loads.

Step 3: Rename the element and add the vendor ID

Change the tag name to the replaceable tag, keep every attribute, add data-termsfeed-pc1-vendor-id, and close the tag with its end tag. Here is a Google Maps embed before and after the swap.

<!-- Before -->
<iframe src="https://www.google.com/maps/embed?pb=MAP_ID" width="600" height="450" style="border:0" loading="lazy" title="Our shop on Google Maps"></iframe>

<!-- After -->
<termsfeed-privacy-consent-tag-iframe data-termsfeed-pc1-vendor-id="termsfeed-1a2b3c4d-5e6f" src="https://www.google.com/maps/embed?pb=MAP_ID" width="600" height="450" style="border:0" loading="lazy" title="Our shop on Google Maps"></termsfeed-privacy-consent-tag-iframe>

Follow these rules for every replaceable tag.

  • Keep every attribute. The real element receives all of them, including src, width, height, title, and allow.
  • Write the end tag. Close <termsfeed-privacy-consent-tag-img> and <termsfeed-privacy-consent-tag-link> too, although <img> and <link> have none. The browser otherwise reads what follows as part of the replaceable tag.
  • Name a tag when you need to find it. Set data-termsfeed-pc1-replaceable-tag-id, such as video-1. Without it, Privacy Consent numbers the tag, such as termsfeed-1a2b3c4d-5e6f-iframe-1. Each tag gets its own embed placeholder either way.
  • Put replaceable tags inside <body>. An unknown element in <head> makes the browser close <head> at that point.
  • Write replaceable tags into the page before it loads. Privacy Consent looks for them once, when it starts, so a replaceable tag your JavaScript adds later is never filled. Handle late content with Single-Page Applications.

Step 4: Set explicit dimensions

Give a replaceable iframe or image its width and height, and reserve the same height on a container around it. The embed placeholder takes the height of its own text and buttons and copies no size from the replaceable tag, while the real element takes the size you set. Here is a container that keeps the space of a 450-pixel map in both states.

<div style="min-height: 450px;">
  <termsfeed-privacy-consent-tag-iframe data-termsfeed-pc1-vendor-id="termsfeed-1a2b3c4d-5e6f" src="https://www.google.com/maps/embed?pb=MAP_ID" width="600" height="450" title="Our shop on Google Maps"></termsfeed-privacy-consent-tag-iframe>
</div>

The container holds the same height before and after the visitor answers. Measure the shift for both states with Prevent Layout Shift.

Publish the page first, and run the check on a visit where the consent banner asks first, such as one from the EU. Under the opt-out model, where no consent banner shows, the element loads at once.

  1. Close every private window, open a new one, open Chrome DevTools, select the Network tab, and type the vendor host in the filter box, such as google.com/maps or youtube.
  2. Load the page. The embed placeholder shows at the place of the element, and no request to the host appears.
  3. Click Accept this vendor in the embed placeholder. The request to the host appears, and the real element loads in place with no page reload.
  4. Open the Elements tab and search for termsfeed-pc1-vendor-tag-id=. The real element sits after the replaceable tag, and it carries that attribute with the tag ID as its value, such as termsfeed-1a2b3c4d-5e6f-iframe-1.
  5. Close every private window, open a new one with the same filter, load the page, and click I Decline on the consent banner. The embed placeholder stays and says the visitor rejected the content, and no request to the host appears.

The element now loads only for visitors who accept its vendor. It stays on the page until the next page load if the visitor withdraws consent.

How to keep inline code in a replaceable script from running early

The browser parses the content of a replaceable tag before Privacy Consent runs, so plain inline code reaches it altered and an element inside it loads at page load. Text inside the tag runs only as the content of the real <script>. A < followed by a letter starts a tag there, and a & followed by a letter or # reads as an entity. A hidden attribute on the tag blocks nothing.

Use one of these forms instead. An external file has no rules for its code, and it reads the attributes of the tag through document.currentScript.

<termsfeed-privacy-consent-tag-script data-termsfeed-pc1-vendor-id="termsfeed-1a2b3c4d-5e6f" src="https://www.example.com/js/chat-init.js" id="chat-init" data-site="42"></termsfeed-privacy-consent-tag-script>

Inline code goes inside an HTML comment, which the browser keeps as written. Put <!-- directly after the > of the tag, start the code on the next line, and put --> alone on the last line before the end tag. The code must not hold -->, it writes </script> in a string as <\/script>, and the tag does not take type="module".

<termsfeed-privacy-consent-tag-script data-termsfeed-pc1-vendor-id="termsfeed-1a2b3c4d-5e6f" id="chat-init" data-site="42"><!--
var cfg = document.currentScript.dataset;
if (window.innerWidth < 768 && cfg.site) { window.chatMobile = true; }
--></termsfeed-privacy-consent-tag-script>

Longer code sits better in a function in an ordinary page <script>, with only the call inside the comment. Either form runs once per page view as soon as the vendor is allowed, including on page load for a visitor who accepted earlier. A visitor placeholder such as @{TF_PC_V1_VISITOR_STATE} in the comment or in an attribute gets the value for the visitor before the code runs. Check it the same way as step 5, with the host of the file or the effect of the code as your filter.

Why a replaceable tag does not convert

A replaceable tag that shows only the embed placeholder, or nothing at all, has one of these causes, in this order of likelihood. Select the Elements tab in Chrome DevTools for each check.

  • The visitor has not accepted the vendor. The embed placeholder is the expected result until the visitor clicks I Agree or Accept this vendor.
  • "Has replaceable tags?" is off. No element with the class termsfeed-pc1-ec-wrapper follows the replaceable tag. Turn the switch on.
  • The vendor ID does not match. Compare data-termsfeed-pc1-vendor-id with the ID under "Important tags", character by character.
  • The tag name is misspelled. The name is termsfeed-privacy-consent-tag- followed by iframe, img, script, style, or link.
  • The element arrived after Privacy Consent started. Content that JavaScript adds later does not get an embed placeholder and never loads, even after an answer.
  • The vendor is inactive. Privacy Consent serves only active vendors, so click "Activate vendor" on the "Edit vendor" page.

An embed placeholder that stays after the visitor accepts needs a closer look at the embed itself. Trace it with Embed Never Loads.