createDynamicPlacement
Overview
Section titled “Overview”Define rules for automatically creating ad placements based on page content. This is particularly useful for content that loads dynamically, such as infinite scroll pages or user-generated content.
Example
Section titled “Example”window.htag.api('1').createDynamicPlacement({ name: 'article-end-ad', xpath: '//article', minDistanceToSimilarAds: 500,});Parameters
Section titled “Parameters”- placementConfig (required): An object with the following properties:
- name: A unique identifier for the dynamic placement
- xpath: An XPath expression that identifies where to insert the ad
- numberOfPlacements (optional): For multiple placements of the same type
- minDistanceToSimilarAds (optional): Minimum distance in pixels to other ads
- urlPattern (optional): A regex pattern to match against the current URL
Return Value
Section titled “Return Value”Returns the Htag API object, allowing for method chaining.
How It Works
Section titled “How It Works”When you call createDynamicPlacement, Htag will:
- Start monitoring the page for elements matching the XPath expression
- When a matching element is found, create a new element after it
- Continue monitoring for new matching elements at regular intervals
Related Concepts
Section titled “Related Concepts”- Dynamic Placements: Learn more about how dynamic placements work
- Placements: Learn about placement configuration