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: