For much of 2017, Walmart (NYSE:WMT) pulled well ahead of its brick-and-mortar competitors in digital sales, growing them more than 50% year over year in each of the first three quarters. But Walmart was ill-prepared for the holiday season when an increase in orders of electronics and more seasonal items created "operational challenges," as management put it. Walmart ended up sacrificing a lot of sales of everyday items, resulting in online sales growth of just 23% year over year in the fourth quarter.

Target (NYSE:TGT) also faced operational challenges in the fourth quarter as demand climbed. Instead of sacrificing sales, however, Target sacrificed profits. Gross margin declined 40 basis points year over year, "reflecting pressure from digital fulfillments costs," management wrote in its earnings release.

Spending more to make a sale today is key to long-term success in online retail. Amazon (NASDAQ:AMZN) historically has always sacrificed profit in the short term to keep its customers happy and coming back over and over again. Walmart and Target need to adopt a similar approach if they want to take on Jeff Bezos' company.

A woman wearing a Shipt shirt pushing a target shopping car outside of a store.

Image source: Target.

Making the necessary investments

Target sold less than $4 billion in goods online last year. That compares to about $68 billion sold in Target stores.

Target is still extremely small compared to Amazon or even Walmart. Amazon sold nearly $197 billion worth of merchandise through its online marketplace, and Walmart sold over $16 billion, according to an estimate from eMarketer. Those numbers include both first-party and third-party sales on their respective marketplace platforms.

In order to grow e-commerce, Target is focused on expanding the ways it fulfills online orders. Over the last year, management has taken steps to improve its supply chain and use more of its stores as distribution points. It acquired Shipt in December, and started offering same-day delivery to members in Louisiana last month. And now it's offering customers the option of picking up their orders at stores without even getting out of the car.

To be sure, Walmart is investing in fulfillment as well. It rolled out free two-day shipping on millions of items last year, and it's rapidly expanding the availability of its online grocery order and pickup. And Walmart is seeing the impact on its gross margin as well, which fell 61 basis points in the fourth quarter, about two-thirds of which is due to pricing and its e-commerce efforts.

But Target's smaller size means its investments in digital fulfillment can have a larger impact on its margins. It's also been more aggressive than Walmart in its investments as it competes to differentiate itself and win by offering more convenience than online-only retailers. All this ought to have an outsized impact on its margins, but the payoff in digital revenue growth (which was higher than Walmart's last quarter) has helped offset that pressure.

Fierce competition

As Amazon continues to gobble up share of online sales, the competition among Walmart, Target, and everyone else vying for a bookmark in people's browsers continues to increase.

High competitive intensity creates the need for increased spending. That's something Walmart appears hesitant to do, but Target is already plowing money into revamping all of its operations, both in-store and online.

If Walmart doesn't follow in the footsteps of Amazon and Target and increase its spending on e-commerce, particularly its fulfillment, it could quickly find itself falling behind its outlook of 40% year-over-year online sales growth -- as well as lagging the competition.

Meanwhile, Target is doing everything it can to grow its share of the e-commerce market, but it's killing its bottom line doing it. Investors have been disappointed with its bottom-line results, but if you consider the longer-term impact of its investments, shareholders should be cheering its top-line growth.

John Mackey, CEO of Whole Foods Market, an Amazon subsidiary, is a member of The Motley Fool's board of directors. Adam Levy owns shares of Amazon. The Motley Fool owns shares of and recommends Amazon. The Motley Fool has a disclosure policy.