Yolanda Jimenez votes early at the California Museum in Sacramento on Nov. 5, 2018. (Rich Pedroncelli/AP)

Andrew Grant, a Republican long shot who ran for Congress near Sacramento, never knew his attacker was in her 20s or that she was spending $39,000 against him as part of a secret experiment.

But the digital ads placed by Soraya Vaezi on behalf of the Democratic group Priorities USA were hard to miss, as they blanketed parts of his district in the final weeks of the campaign: basic stop-motion videos that accused him of siding with donors to cut taxes for the “ultra rich” and cut health-care coverage for millions.

“They posted on Instagram and were shared with me,” said Grant, who was outraised by his opponent, Democratic incumbent Ami Bera, 5 to 1 and lost his election by 10 percentage points. “I didn’t have a way of telling people otherwise.”

Grant was a guinea pig in a much larger effort by Priorities to improve the way Democrats compete digitally in elections. In addition to spending about $50 million on digital ads this cycle, the group ran experiments behind the scenes to create a new playbook for liberal groups as they rush to catch up with GOP advantages online.

The goal was to bridge the gap with Republican campaigns, which have spent a higher percentage of their election money online in recent cycles. Though they narrowed the gap this cycle, Democrats still say they were outspent digitally — even in a year when Democratic money flooded the political system. An analysis by Priorities of outside groups estimated that Democrats spent $86 million online this cycle, compared with Republican spending of $115 million.

“It wasn’t enough for us to just go spend money on digital. We wanted to encourage, incentivize, shame, educate other organizations to do the same thing,” said Guy Cecil, the chairman of Priorities. “We wanted to be able to show other people it worked.”

Without public notice, Priorities ran tests on Facebook’s secret algorithms to try to decode how long an ad could run before its frequency was diminished in news feeds. It created a dashboard so other liberal groups could legally coordinate their ad purchases with minimal overlap. And it ran extensive testing on the types of advertising that worked best, often discovering that what staffers believed would work did not.

“More often than not we were wrong about what was going to be the thing that worked best,” said Rob Flaherty, the group’s creative director.

Language about bipartisanship tested well in polls but performed poorly online. “People want you to be bipartisan problem solvers for the things that they like,” Flaherty said. “And so often that message would fall flat when you tested an execution of it because you are not saying anything that affects people’s lives.”

Messaging on President Trump to minority voters could discourage Democratic voting by reminding people why they wanted nothing to do with the process. Even an ad that featured a woman giving herself a pep talk in the mirror created a backlash, because people did not want to see someone brushing their teeth or using a Q-Tip.

In perhaps the most ambitious experiment, Priorities set aside about $100,000 to explore a basic problem that confronts many House campaigns that lack the budgets to hire digital consultants. The question: Could young staffers with a few months of basic training design and execute an effective digital campaign on their own?

Two races were targeted. Vaezi, a recent graduate of the University of Wisconsin, focused on Grant’s race in California, and Charlotte Robertson, a recent graduate of Ithaca College, targeted the seat of Rep. F. James Sensenbrenner Jr. in the northern suburbs of Milwaukee. The ads were aimed at registered voters who cast ballots in recent elections but appeared in only half the precincts in the district. That way, the effectiveness of the campaign could be measured against a control group’s election results.

“Before this, Charlotte and I had never used most of the software we were using to develop the creative,” Vaezi said.

“By most of it, she means all of it,” Robertson added.

Both had help from a team at Priorities that split the precinct data so they could make ad purchases themselves on platforms such as Facebook and Google. They also received basic research about what messages could work well in the districts.

The ads against Sensenbrenner also attacked him as voting to “strip health care” by supporting a repeal of President Barack Obama’s Affordable Care Act and claimed that he favored out-of-state donors. A second set of ads had a positive introductory message about his Democratic opponent, Tom Palzewicz, who received about 38 percent of the vote on Election Day, compared with Sensenbrenner’s 62 percent.

The preliminary results of both tests, however, suggest the experiment was effective. The precincts that were targeted moved toward the Democratic candidates by between 0.8 and 1 percentage point over the control group. Priorities estimates the cost per vote won through the advertising to be about $40.

“The lesson is you can do digital,” said Danielle Butterfield, the director of advertising at Priorities. “Anyone can do digital.”

It’s a message that other consultants have been pushing as they try to persuade the Democratic Party to spend more money on digital campaigns next time.

“For 2018, Democrats still had in a lot of places a very traditional media mix,” said Mark Jablonowski, the managing partner of DSPolitical, a Democratic firm that has its own low-cost, self-serve product for smaller campaigns. “I worry that when we had a blue wave that raises all ships that people could take the wrong message away from it.”

Back in California’s Central Valley, Grant had less praise for the efforts against him, which were concentrated in a single message attacking him as supporting cuts in health coverage to give tax cuts to campaign donors. He was in favor of the Republican tax bill criticized in the spot, legislation aimed at repealing the individual mandate meant to pay for coverage for uninsured Americans. But he said he did not support repealing the ACA without a replacement, as the ad that criticized him implied. He also said the ad suggested he had voted for the tax bill, even though he does not hold elective office.

“They were misleading,” he said. “It’s a lie.”

But he said he was not surprised to see the ad show up. “I was actually surprised to see they didn’t come after me more,” he said.