Twitter and Facebook helped 'Ted' become a surprise hit. Universal Pictures

The teddy bear's first tweet, from an account called @WhatTedSaid set up by the Universal Pictures marketing department, was "Hello, Twitter. Kindly go f— yourself."

The author of the greeting was Alec Sulkin, co-screenwriter of the R-rated comedy "Ted," who together with his collaborator Wellesley Wild was paid extra by the studio to build buzz on social media ahead of the film's June 29 release. Who better to embody the random musings of a foul-mouthed stuffed animal than the writers of the script? The suits left them alone.

"The parameters were, 'Just go to town,' " says Doug Neil, Universal's senior vice president of digital marketing. The tweeting started March 30, two days before the "red band" (uncensored) trailer appeared online, depicting the namesake bear smoking weed, cuddling with co-star Mark Wahlberg and pantomiming suggestive acts for a supermarket checkout girl.

Mr. Wild was in charge of blogging "Ted's Fuzzy Thoughts," a collection of ravings on subjects such as whether microscopic robots slathered on meat could cure cancer. Meanwhile, Mr. Wahlberg changed his Facebook cover picture to a banner saying "ted is here," and began posting content like a not-safe-for-work video birthday message from the bear for his 600,000-plus fans to share.

Mark Wahlberg, Mila Kunis and Seth MacFarlane star in MacFarlane's first feature-length comedy "Ted" about what happens when a boy and his stuffed bear grow up. Watch a clip from the film. Video: Courtesy of Universal Pictures.

It worked spectacularly. Tracking polls, which movie executives rely on to guide box office expectations, suggested an opening-weekend gross of $35 million to $40 million for the film, which was co-written and directed by Seth McFarlane, creator of "Family Guy," who also provided the voice for Ted. Instead, "Ted" generated $54 million, catching the industry by surprise.

Hollywood is doing more than using Twitter and Facebook as mere promotional tools. After several years of experimenting, studios have thrown themselves deeply into a medium which is still barely understood. They are now developing elaborate social media campaigns early on, sometimes as soon as a film gets greenlit. Researchers are conducting deep numerical analysis on posts and tweets to guide marketing decisions, sometimes predicting box office revenue with pinpoint accuracy. They're looking not just at opening movies, but sustaining their word-of-mouth through subsequent weeks. And they are getting more surgical about targeting their ever-fickle, ever-elusive core audience of young people.

Movie marketing has always been something of a black art. Studios typically intensify advertising the month before a movie opens, spending heavily on a barrage of television spots. Upcoming films are now surfacing on social media far earlier. On July 14, nearly a year before the release of M. Night Shyamalan's "After Earth," the producers released a video in the form of a Facebook timeline using headlines and photos to describe the historical run-up to an alien-driven apocalypse (the film stars Will Smith).

The coming 'The Hobbit: An Unexpected Journey' has high socialmedia buzz. Warner Bros. Pictures

For Paramount's coming film, "Paranormal Activity 4," the studio is working with Facebook for what it calls the "Want It" campaign, allowing fans to request a premiere in their hometown, anywhere in the world; the 25 cities with the most voting activity will get it first. For "Pitch Perfect," a comedy debuting Oct. 5 about an a cappella singing group, Universal is announcing a competition on Monday in which fans can submit videos of themselves singing Nicki Minaj's hit song "Starships." The winning clips will be spliced into a music video featuring the cast of the film and Mike Tompkins, an a cappella artist whose YouTube page has more than 500,000 subscribers.

There is mounting evidence that promotions like this do move the dial. Last year, Paramount executives were worried about lukewarm buzz over "Super 8." They'd expected more enthusiasm for a movie with Steven Spielberg attached as a producer, and fan-boy favorite J.J. Abrams as the director. The problem, they felt, was the unusual tone of the movie, part family drama, part sci-fi horror flick. It was "hard to capture in a 30-second TV spot," says Amy Powell, Paramount's president of digital entertainment.

'Brüno' was hurt by negative Twitter comments, creating what studios now refer to as the Brüno Effect. Universal Pictures

The Monday prior to the film's release, as disappointing tracking numbers continued, Ms. Powell contacted Twitter Chief Executive Dick Costolo and revenue chief Adam Bain with the idea of using Twitter to announce, via sponsored tweets, early Wednesday screenings at 300 theaters. And they threw in free popcorn. After the tweets went out, they sold several million dollars in tickets.

As fans emerged from the Wednesday night screenings, they tweeted mostly positive reactions with the #Super8Secret hashtag. The 11th-hour blitz paid off: the film opened to $35 million, a better-than-expected result that Paramount Vice Chairman Rob Moore credited to the Twitter campaign.

Kristen Wiig in 'Bridesmaids' Universal Pictures/Everett Collection

Sacha Baron Cohen in 'The Dictator' Paramount Pictures/Everett Collection

No studio has been bold (or foolish) enough to completely abandon television advertising in favor of a strictly social-media approach. Studio executives say it's as yet impossible to determine how much a social media strategy actually contributes to a film's box office results. But the promise for studios is that it could lower marketing costs.

"No matter what a film costs, if you're going to go super-wide you have to go there," says veteran distribution executive Bob Berney, referring to big TV ad spends. "The question is: by [using social media], can you do it for $10-or-more million less and get the same result?"

Twitter now has 140 million active users, half of whom log in every day, while Facebook is the largest social network, with nearly one billion users world-wide. About a quarter of the most influential moviegoing audience—18-to-29-year-olds—tweet, nearly twice as many as in older age groups, according to the Pew Internet and American Life Project.

Stanley Tucci and Jennifer Lawrence in 'The Hunger Games' Lions Gate

Several studios, including Paramount and CBS Films, have begun using tools devised by a five-year-old Boston company called Crimson Hexagon. Founded by a Harvard professor, the company has access to Twitter's entire "fire hose," every public tweet that's ever been written since 2010. The company's algorithm analyzes "unstructured text"—the colloquial way people speak on social media—to answer specific questions about consumers' desire for various products, including movies. For example, it can search for tweets, blogs or Facebook comments from people comparing "Ted" to "The Hangover."

a U.S. Navy SEAL in 'Act of Valor' Relativity Media

Crimson Hexagon has about 100 customers across industries, ranging from General Motors to Twitter itself, who pay more than $100,000 a year for access to its intelligence. In a conference room in a tower overlooking Boston Harbor, a team of Crimson Hexagon executives demonstrated how recent research revealed that social media comments are tightly correlated to opening-weekend box office. By looking at the volume of tweets expressing "intent to view" (such as "I'm dying to see the new Bourne film!") across seven different movies—including "The Hunger Games," "The Dictator," "Act of Valor" and others—they observed a 98.8% correlation with actual gross revenue per theater. Chief Executive Patricia Gottesman cautioned that it's unclear to what extent social media is creating, as opposed to responding to, the buzz about a given film.

Fizziology, an Indianapolis-based firm that analyzes social media, examines the volume and sentiment of comments to supplement traditional tracking, advise on casting decisions and see what people are buzzing about. The company has developed some rules of thumb: 70% or more "positives"—favorable comments about a movie or its trailer—bodes well for a film's opening. Both "Ted" and "The Avengers" were hovering in that range. Negatives aren't necessarily bad—deliberately provocative horror films weather them just fine, says Fizziology Chief Executive Ben Carlson. But having mostly neutral comments can be the kiss of death. For example, "Battleship," a colossal flop, saw only 30% positive comments, with the rest being mostly neutral.

Looking at the overwhelming (90% positive) buzz surrounding "Ted" three weeks before opening weekend, Fizziology predicted that opening weekend box office results would be between $39 million and $55 million, as opposed to the $35-40 million that the studio was expecting based on traditional tracking—closer to the $54 million that the film wound up bringing in.

Early Interest: In addition to 'The Hobbit' (far left and fourth from left), coming movies that are the subject of anticipatory chatter on social media include 'The Twilight Saga: Breaking Dawn—Part 2,' the latest James Bond, 'Skyfall,' and 'The Great Gatsby' with Leonardo DiCaprio and Carey Mulligan. Photo Illustration by Ryan Etter; Summit Entertainment (Twilight Saga); Warner Bros. Pictures (‘The Hobbit’, ‘The Great Gatsby’); Columbia Pictures/EON (‘Skyfall’)

On the flip side, studios worry that once a film is released and the marketing dies down, negative comments can discourage people from buying tickets. Some 72% of social media users immediately post opinions on social sites after watching a film, and 8%, annoyingly, do so while a film is actually playing, according to a recent survey conducted by Penn, Schoen and Berland for the Hollywood Reporter. "Word of mouth spreads much faster, both positive and negative, than ever before," says Ira Rubenstein, Fox's executive vice president of digital.

Some in Hollywood have dubbed this "The Brüno Effect," after the 2009 Sacha Baron Cohen comedy about a flamboyantly gay Austrian television host. The raunchy R-rated film generated $14.4 million in its opening night, but then witnessed a surprisingly steep 39.2% drop-off in receipts on Saturday. A group of Harvard graduate students specializing in statistics in 2010 examined whether Twitter was responsible for the decline. They eventually determined that there was a relationship between the proportion of negative tweets like "Bruno movie is probably the worst I ever seen" and the decline in revenue from Friday to Saturday. "What we found is that it may be hard to put a value on word-of-mouth but that it absolutely has predictive power," says Omar Wasow, a Ph.D. candidate who worked on the study.

Crimson Hexagon also found evidence of the Brüno Effect: in the two week period following opening weekend, the percentage of people saying that they "won't see" the film doubled to 31%. By contrast, the proportion of people who said they didn't want to see "Bridesmaids," a sleeper hit with positive word-of-mouth, remained under 5%.

Mr. Neil says that "Brüno," which also was a Universal film, "had a great campaign and a strong opening night." Nevertheless, the opening-weekend experience was emblematic of the growing importance of "the immediate reaction" on social media sites, he says.

To get reactions to "Ted" on Facebook, the studio posted questions like "Do you think Ted is the new Citizen Kane?" and offered apps like "My Wild Nights With Ted," that enabled people to import an image of the bear into their own photos, and post them on their personal Facebook pages. The studio even found itself embroiled in a social-media smackdown with Warner Bros., which was opening "Magic Mike," a Steven- Soderbergh-directed movie about male strippers, on the same date. After seeing an e-card posted on the "Ted" Facebook wall showing the teddy bear grasping a stripper pole and exhorting fans to "spend time with Tantalizing Ted" instead of "Magic Mike," Warners fired back, using the "My Wild Nights With Ted" app to insert "Ted" into a photo of their film's shirtless ensemble cast, writing "Even 'Ted' wants to see 'Magic Mike.' " In the comments section of the official "Magic Mike" Facebook page, "Ted" wrote—using unprintable language—that he had no interest in looking at naked men.

Universal was also monitoring what people were saying about the film, and adjusting online content to please them. For example, soon after the first "Ted" trailer went online, Fizziology observed that a lot of the online chatter about it centered on the "Thunder Buddies" song, which Mr. Wahlberg sings with the bear while in bed. Mr. Neil's team immediately jumped on it, adding more content, like a remixed version of the song that plays on the TedIsReal.com website, e-cards with lyrics on Facebook, Thunder Buddy pajamas available via CafePress.com, and a 30-second video clip of the song. When the film's chief financial backer, Modi Wiczyk, heard ESPN radio use "Thunder Buddies" in a bit about the Oklahoma City Thunder and the Miami Heat in the NBA finals, he asked the studio to push the song even harder.

By the time opening weekend for "Ted" rolled around on June 29, the @WhatTedSaid account had over 200,000 followers (and is now double that), an impressive number considering that Ted's musings were so obscene that the Motion Picture Association of America refused to allow the studio to promote the account on posters. And the proportion of conversation that came from merely re-tweeting the @WhatTedSaid account had fallen by half to 19%, "indicating that more organic opinions have formed," Fizziology wrote in a memo to Universal. In other words, potential moviegoers had been exposed to enough information to form their own views, not just follow the pack.

The film's Facebook page had about a million fans; it now has more than 2.7 million. "Talking Ted," which allows users to control the bear's movements and statements, became the No. 1 entertainment app on iTunes and was downloaded 3.5 million times. Excluding sequels, "Ted" is now the most successful R-rated comedy of all time and this weekend should surpass $200 million in domestic box-office revenue.