Adventures with Ad Delivery in Rails

It’s always hard to find the line between buy vs. build - or as it’s known in the open-source world, buy vs. “borrow.” I find this to be more true in Rails than in any other application framework. You want authentication, you got it; you need paging, done. Before you know it you’ve got an application that does almost everything you need, almost everything you don’t and you’re stuck in the middle.

Now, it’s time to actually write code.

At, we had a problem on our hands. We needed to deliver ads from our own inventory - space on the site that our sales team sells to our clients. I immediately asked - how can we do this without writing anything. You can anticipate the complexity that comes from adding a feature like that: find ads contextually, figure out which one to show, show it, track that it was shown, all while not slowing down the page. We take performance very seriously at

So we tried on Google AdManager for size. It seemed to be the right fit. First off, it’s Google, so it’s gotta be good (debatable, I think). Secondly, it promised to do all the heavy lifting for us: tracking impressions, click-throughs, reporting, delivery, impression distribution, inventory management, and more. They even worked with us to open up an advanced custom attribute targeting system. The only known draw back was that it wasn’t going to work with other ad network APIs. Plus, it was a drop in replacement for AdSense - so we had nothing to loose.

A few weeks later, a day before one of our clients ads had been promised to be delivered, AdManager has yet to perform as we’ve expected. The interface is clunky and convoluted. I can’t blame them, they’ve built a behemoth catch-all advertising management system. But most importantly it wasn’t delivering the ads we needed to satisfy our customers (I later discovered that AdManager requires several weeks for custom targeting data to propagate and effectively deliver ads.)

With our lovely sales director popping her head in to make sure we on target for ads to be delivered, I found myself between a rock and a hard place. Then it occurred to me: “wait, I know how to write code!”

Queue Pandora. I started with a basic ActiveRecord model and stubbed out what I thought we’d need - borrowing heavily from my experience on the Google side of the wall. The biggest issue was performance and although I was reluctant to have ad delivery happen synchronously, I decided to go with it (we’d have some time to refactor before we had enough inventory to slow things down). I had already written a nifty tracking class to help me keep track of the context of the page such as keywords and geography. In an afternoon I had the whole thing done.

Pretty soon it was in production and getting feedback from sales people and customers. There were bugs, as was to be expected, but we’ve since worked them out in addition to adding several new features. Shameless plug: All you small business owners out there may be interested in our new geo-targeted ad product.

The moral of the story was that sometimes - I’ll say maybe even the majority of the time - it pays just to write it yourself. To borrow from my friend Michael Dwan, and then in turn DHH, “don’t use code you couldn’t write yourself.”

Next up, Google Analytics.

Thanks for reading! I'm Avand.

I’ve been working on the web for over a decade and am passionate about building great products.

My last job was with Airbnb, where I focused on internal products that helped teams measure the quality of the software they were building. I also built internal tools for employees to stay more connected, especially after the COVID-19 pandemic. Before that, I was lead engineer at Mystery Science, the #1 way in which science is taught in U.S. elementary school classroms. For a while, I also taught with General Assembly, teaching aspiring developers the basics of front-end web development.

I was born in Boston, grew up in Salt Lake City, and spent many years living in Chicago. Now, I call San Francisco my home and Mariposa my home away from home.

I enjoy the great outdoors and absolutely love music and dance. Cars have been an lifelong obsession of mine, especially vintage BMWs and Volkswagens. I’m the proud owner of a 2002 E-250 Sportsmobile van, and he and I have enjoyed many trips to beautiful and remote parts of the West Coast to create good vibes.

What can I do for you?

Read my other posts or get in touch: