When will companies learn the golden rule: Think before you tweet.

Keurig and other brands caught flak from all sides for how they responded to social media calls to distance themselves from Fox News host Sean Hannity. Companies walked back statements they made on Twitter or struggled to explain their actual relationships to Hannity -- in each case stoking the social media fires.

Critics targeted companies that advertised on Hannity's syndicated radio show as well as his Fox News program after Hannity appeared to defend Alabama Republican Senate candidate Roy Moore on Thursday. Moore has been accused of sexual misconduct with teenagers, including a 14-year-old girl. He has denied the allegations.

The firestorm began in earnest on Friday. Angelo Carusone, president of liberal watchdog group Media Matters for America, appealed directly to brands like Keurig to cut advertising ties with Hannity.

"Good afternoon @Keurig. You are currently sponsoring Sean Hannity's show ... please reconsider," Carusone wrote on Twitter.

Keurig responded the next morning. The company said on Twitter: "We worked with our media partner and FOX news to stop our ad from airing during the Sean Hannity Show."

Angelo, thank you for your concern and for bringing this to our attention. We worked with our media partner and FOX news to stop our ad from airing during the Sean Hannity Show. — Keurig (@Keurig) November 11, 2017

Keurig's response was praised by Hannity's critics. But it sparked a backlash from Hannity's supporters, who started a #BoycottKeurig hashtag and, in some cases, even smashed their own Keurig machines.

By Monday, Keurig CEO Bob Gamgort had apologized for how Keurig responded.

"The decision to publicly communicate our programming decision via our Twitter account was highly unusual," Gamgort wrote an internal memo to employees. "This gave the appearance of 'taking sides' in an emotionally charged debate that escalated on Twitter and beyond over the weekend, which was not our intent."

Keurig wasn't the only company to walk back its initial response to the Hannity controversy.

Realtor.com tweeted on Saturday "we are not currently, and will not be running TV ads on Hannity." But it later deleted the tweet, and on Sunday it posted a statement to its corporate blog with a very different message: "We will continue to place ads across a broad range of networks, including Fox News and its top shows."

Related: Papa John's says NFL protests are hurting sales

Reddi-wip, which is owned by ConAgra (CAG), tweeted on Monday "our objective has always been to reach fans in ways that align with our values. Therefore, we are removing our ads from the show," in response to a user who asked the brand not to support Hannity. Later, the company said "we removed Hannity from our advertising plans," adding on Tuesday, "this program has not been included in our media plan for a long time."

A ConAgra representative confirmed on Tuesday that the company has not advertised with the program for months, but added that the controversy hasn't impacted ConAgra's future plans.

Irv Schenkler, Director of the Management Communication Program at New York University's Stern School of Business, said that companies need to take a balanced approach when developing their social media strategies. On one hand, firms should be engaging with their customers online. On the other, they should be wary of jumping into a controversy too quickly, he advised.

Sometimes when companies tweet "they are acting from the seat of the pants, as opposed to taking a moment to analyze and examine the dimensions of the event or issue," Schenkler explained.

By responding too quickly on social media, companies may end up exacerbating controversies that may fizzle out on their own, he said.

Related: Equifax turned its hack into a public relations catastrophe

Brayden King, a professor of Management and Organizations at Northwestern's Kellogg School of Management, said Twitter can be an easy way for brands to get messages to a large audience. "Twitter reaches a lot of people very quickly," he said, while "a press release can be ignored by the very people you want to see it."

But companies do face a risk when they use social media platforms to disseminate a position. "If you don't think through the media strategy carefully, you can expose yourself to criticism from other people -- including people you see as potential customers," King said.

Schenkler added that brands may sometimes forget how public their Twitter interactions are.

"What [brands] might consider to be business conversations are just out there, and people forget that," he said. "And they pay the price sometimes."

To protect themselves, Schenkler said, companies may want to enact a social media process or protocol that prioritizes the brand's ultimate objectives -- and keep it in mind when responding to a controversy.