Best way to randomly serve different ads in CMS list?

Hello!

I want to show ads in a CMS list, meeting a few criteria:

  1. One (1) ad slot inside of my list, on the first page.
  2. I want to able to run more than one ad campaign at once, switching randomly between the different ads that are inserted in the list. So that the advertisers get a roughly equal share of the inventory (views).
  3. I need to be able to measure the views (Google Analytics is set up).
  4. This… without messing with my CMS Filter, Load, Select, Custom select… and so on.

I’m picturing a separate CMS-collection with the ads, but I can’t figure out how to get them inside of another list in a fixed place.

The use case:
I’m building a directory – a list where corporate events (chapter 11 filings) are served from a CMS collection.

Happy for any ideas,
Thanks
Jacob

Hey @Jacob_B!

We could use some custom code to achieve this if you are already using CMS Load

What I am thinking is to randomly select the item from the hidden list and use the API to add the item either to a set place or randomly.

If you can share a link I can work on the code! :muscle:

Hey there @Support-Luis ,
Thank’s for getting back to me!

Here’s a link to the project.

Your idea sounds great, as it would enable me to keep the ads in a separate CMS collection. Let me know if you need anything else.

Really thankful for your help!
Jacob

Thank you! This is the code that should handle this. You can add it to the </body>

<script>
  window.fsAttributes = window.fsAttributes || [];
  window.fsAttributes.push([
    'cmsload',
    async (listInstances) => {
      console.log('cmsload Successfully loaded!');

      const [listInstance] = listInstances;
      const adsList = document.getElementById('ads');

      // Function to get a random child element
      function getRandomChildElement(parentElement) {
        const children = parentElement.children; // Get all child elements
        const randomIndex = Math.floor(Math.random() * children.length); // Select a random index
        return children[randomIndex]; // Return the random child element
      }
      const randomAd = getRandomChildElement(adsList);

      // Clone the random ad element
      const clonedAd = randomAd.cloneNode(true);

      // Prepare the static item to add
      const staticItem = {
        itemElement: clonedAd, // Cloned ad element
        targetIndex: 5, // Define the place to add the ad
        interactive: true, // Set to true if the item should be interactive
        repeat: 0, // No repeat in this case
      };

      await listInstance.addStaticItems([staticItem]);
    },
  ]);
</script>

You would only need to add the ads collection to the page and assign the ads ID to the collection list element.

I added some comments to the code to explain what it does, but if you need any help please reach out!

HI @Support-Luis , many thanks!!
I tried it quickly, and it seems to work perfectly. I’m going to toy around with it – I’ll get back to you.
Thank you SO much,
Jacob

1 Like

Awesome! Let me know how it goes or if you need any more help! :muscle:

Hi again @Support-Luis ,

I have two questions regarding the script you provided. To refresh your mind, it inserts an ad in a collection list.

Firstly – regarding load times: Although it works nicely, the ad takes a second or so to load. It loads after the list in which the ad is inserted, and the effect is that once it loads it knocks down the other items in the list. It is not a big deal, but I’d obviously prefer if it would load faster. Any thoughts?

Secondly – as of now, the ad disappears whenever you start entering information in the search filters. Is there a way to “pin” the ad to the top, regardless of what is being entered in the filters?

Many thanks!
Jacob

Hey @Jacob_B!

For the first issue, we could try to

For the second issue, currently, there is a bug for the static element that requires the element to be interactive, which is what makes the filters affect the element.

You can set the interactive parameter to false to prevent the item being affected by the filtering, as shown below

const staticItem = {
            itemElement: clonedAd, // Cloned ad element
            targetIndex: 0, // Define the place to add the ad
            interactive: false, // Set to true if the item should be interactive
            repeat: 0, // No repeat in this case
          };

This bug shows up on the Static to Collection List Attribute so please verify that the last element is correctly shown.

This issue will be addressed in V2, which we are close to release