New Hampshire Senator Kelly Ayotte’s most memorable blunder in her re-election campaign came in an October debate, when she said she would “absolutely” hold up Donald Trump as a role model for children. But her most important miscalculation might have come three and a half years earlier.

In April 2013, Ayotte voted against a bipartisan bill brought by Republican Pat Toomey of Pennsylvania and Democrat Joe Manchin of West Virginia that would have required background checks for almost all gun sales. Her opposition helped defeat the legislation, which was drafted in the wake of the mass shooting that left 20 children and six adults dead in Newtown, Connecticut. Ayotte announced her stance at the last minute, angering the bill’s proponents, who were holding out hope for moderate Republicans to pass the legislation.

Ayotte, then midway through her first term, was already one of the most vulnerable Republicans up for reelection in 2016. After her vote against Manchin-Toomey, gun safety advocates made defeating her a priority. Independence USA, former New York Mayor Michael Bloomberg’s political action committee, put more than $6 million toward that effort. Americans for Responsible Solutions, the group lead by former Congresswoman Gabby Giffords, spent about $2.6 million opposing Ayotte.

On November 8, Ayotte fell to Governor Maggie Hassan, a Democrat, by just 743 votes, about one-tenth of 1 percent, in a race where roughly 700,000 votes were cast.

Her loss was one of the bright spots for gun reformers in the 2016 campaign. The National Rifle Association saw the record-breaking financial bet it placed on Trump’s bid result in a shocking White House win, while going five for six in the Senate races in which it spent more than $1 million, helping Republicans hold the chamber.

To show how Ayotte’s opposition to tighter gun laws hurt her with voters, Independence PAC and ARS shared data with The Trace that they say indicates their attacks were the race’s deciding factor. But the number that may command the most attention in political circles is the hefty sums that the groups dropped in their effort to oust Ayotte at any cost.

In the late spring and summer, ARS and Independence USA aired ads that blasted Ayotte’s background check vote and positions on barring terror suspects from buying guns. But in the race’s last few months, Independence USA used its biggest expenditure of the contest to deploy commercials that eschewed the gun issue for other messages with potential to motivate women and independents to cast their support to Hassan. (Independence USA operates independently of Bloomberg-backed Everytown for Gun Safety, a group that is a funder for The Trace.) Focused on making an example of the senator, the PAC did not constrain itself to its core policy beef with Ayotte as it poured on the spending.

Liz Johnson, a spokeswoman for Ayotte’s campaign, declined to address questions about the campaign’s outcome. But Ayotte aides privately noted that their side was outspent by about $17 million on television ads. “We attribute the defeat to that drastic resource gap,” one said.

In an email, Bloomberg political aide Howard Wolfson said that Ayotte could have spared herself the onslaught. “The polling is clear she was on the wrong side of [New Hampshire] on guns,” he wrote. “It’s also fair to say that had she voted yes on Manchin-Toomey, every dollar that we spent against her would have been spent for her.”

It’s hard to definitively conclude how much any interest group is able to change voters’ minds. Elections are decided on many issues, and the top of the ticket matters immensely, especially in an election cycle as polarizing as 2016. The presidential race was similarly tight in New Hampshire — Hillary Clinton beat Trump by roughly 2,700 votes — which suggests that partisan identification, not firearms or other policy issues, was the most important factor in the contest, said Andrew Smith, director of the University of New Hampshire Survey Center, who oversaw extensive polling in the state. “That’s a more realistic explanation than any one issue,” Smith said.

But when a race is as tight as Ayotte’s was, groups can also persuasively declare their outside spending to be successful even if it swayed only a few hundred people — in this case, by denting Ayotte’s self-styled image as a lawmaker who charts her own course.

A former prosecutor, Ayotte cultivated a nonpartisan brand that won her backing from Democrats and Republicans during much of her first term. Her supporters and many pollsters expected the senator to have enough crossover appeal to win if the presidential race in the state stayed close, Smith said. But in the end, Ayotte barely outperformed Trump.

“She stands with the Washington gun lobby — they fund her campaigns,” intoned a narrator in an ad aired by ARS in New Hampshire from May 26 to June 8. The ad ripped Ayotte of allowing “criminals and the dangerously mentally ill can buy guns without a background check,” even though “nearly nine of ten Granite Staters would close that loophole.” ARS reported spending about $740,000 on media buys for the spot.

Zach Stewart, political director for ARS, said the timing of that ad — which came during a pause in the deluge of campaign commercials that hit New Hampshire during the height of the primaries and the home stretch of the general election — helped home in voter’s attention. “We were able to get the electorate to really focus on the issue of gun violence prevention,” said Stewart.

The group’s pollsters wrote in a memo viewed by The Trace that 41 percent of voters had a negative impression of Ayotte before the spot ran. After, her unfavorables rose to 46 percent, and Hassan widened a lead over Ayotte from two to four points, according to the polling.

Democratic campaign consultant Douglas Schoen, who does polling for Bloomberg, wrote in a separate memo shared with The Trace that his polling found 54 percent of Granite Staters said they were less likely to back Ayotte in response to ad scripts faulting Ayotte’s background check vote. Sixty-four percent were less likely to support her after hearing a different ad script citing her change of position on legislation barring people on a federal terror watch list from buying guns.

The second script referenced a position that Ayotte struck in December 2015, following the mass shooting by ISIS sympathizers in San Bernardino, California, when Ayotte opposed a Democratic proposal that would have added persons on the consolidated terrorist watch list to the roster of those prohibited from firearm ownership. In July, following the attack on Orlando’s Pulse nightclub — perpetrated by a gunman who’d once been investigated for possible terror ties — Ayotte cast a procedural vote in favor of the same measure in what seemed to be an effort to moderate her gun safety record. She also co-sponsored a bipartisan plan with Maine Republican Susan Collins that aimed to address concerns about other partisan bills on the issue.

An ad from Independence USA that ran in the beginning of October, at a cost of $1.4 million, bashed Ayotte for “flip-flopping” on the terror gap.

Strategists for the gun safety groups said that Ayotte’s record on gun safety was a potent issue with Democrats especially, but also independents. “We did test this compared to other hits like taxes, cap and trade, etc. and when we started with this line of attack (‘Ayotte voted against background checks’) it moved the race significantly more than if this was just another hit among the others,” Stewart said in an email.

Schoen said that polling found ads faulting Ayotte’s opposition to background checks resulted in 62 percent of voters leaning toward Hassan saying they were less likely to back Ayotte. Fifty-seven percent of voters leaning toward the Republican and 58 percent of undecideds reported the same after viewing the spot.

Schoen wrote that ads saying Ayotte “sided with the gun lobby to make it easier to get guns” were as effective as ads attacking her for saying she would tell a child to be like Trump, according to focus group research. Both increased support of Hassan by 4 percent and reduced support for Ayotte by five points.

Independence USA hit Ayotte for her loyalty to the NRA in a digital ad that cost up to $63,000, a relatively small sum, but when its high-profile television ads began airing in October, the PAC focused on different issues. An $1.4 million buy in tied Ayotte to Trump. Another, worth $3 million — Independence USA’s biggest expenditure in the race — highlighted Ayotte’s six votes to defund Planned Parenthood.

Wolfson, the Bloomberg aide, declined to say why the PAC focused on issues other than guns. But its tactics suggest that when gun politics draw the organization into a race, it will use whatever messaging it deems effective to get its desired result. That’s not an uncommon approach for single-issue groups: The NRA ran an ad last summer that faulted Clinton for the terror attack at Benghazi without one mentioning gun policy. In both cases, the goal is to make politicians fear the animosity of the interest group, as much as it is to sway or mobilize voters, said Travis Rideout, a political scientist and co-director of the Wesleyan Media Project, which tracks political advertising.

“Knowing someone out there will big pockets is willing to attack you if you vote the wrong way is something that members of Congress are constantly worried about,” Rideout said.

The NRA endorsed Ayotte but provided her with few financial resources — even as the gun control groups hammered her. The NRA may have failed to come to Ayotte’s aid because she broke with the group by supporting Collins’s bill. The NRA did not respond to requests for comment.

Despite her vote against expanded background checks, New Hampshire’s homegrown gun rights groups showed little love to Ayotte. One such organization faulted her vote to confirm Attorney General Loretta Lynch and for other steps they said showed failure to embrace a gun rights agenda. That opposition may have boosted independent candidate Aaron Day, who drew 2.4 percent of votes in November, and Libertarian Brian Chabot, who won 1.8 percent. Both candidates took more unabashedly pro-gun stances during the race — and both drew more votes than the difference between Ayotte and Hassan.

Stephen Duprey, a former head of the state’s Republican Party, said the election results suggest that Ayotte’s differences with extreme gun rights backers was as important as the opposition from gun safety groups. By taking a position that drew attacks from the groups but little help from the NRA, Ayotte was left exposed, Duprey suggested.

Gun safety groups, for their part, have already concluded that their New Hampshire effort can serve as a model for upcoming contests. While the groups declined to identify specific contests they hope to target in 2018, ousting Senator Jeff Flake, an Arizona Republican who also voted against Manchin-Toomey, is one likely priority.

“This is absolutely replicable,” Stewart said.

[Correction: This story originally misstated the amount of money ARS spent on the race. It was $2.6 million, not $3.5 million.]

[Photo: AP Images/Elise Amendola]