The Wisconsin Senate race was an afterthought in mid-September: Polls showed Republican Sen. Ron Johnson trailing, while both parties scrambled to pour money in states they considered more competitive.

But to top officials in Johnson’s campaign, the middle of September is exactly when they became convinced they would win — thanks in part to a data operation that had a better understanding of the electorate than most.

Johnson’s defeat of Russ Feingold was, even for this year’s election, a shocking result. The popular former three-term Democratic senator looked like a shoo-in for victory from the moment he entered the race last May.

But despite his uphill fight, Johnson had the benefit of a sophisticated data operation inherited from the state’s GOP Gov. Scott Walker, one that gave the campaign a clear-eyed look at an unpredictable electorate. Those tools — which the campaign worked to update and improve — didn’t win the incumbent re-election, obviously, but they did position him to take advantage of a better-than-expected political environment.

And they helped Johnson’s team weather dark moments like the one in September, when officials on the campaign knew they were closing in on Feingold despite the palpable pessimism of others.

“It doesn’t matter what anyone else says or does right now, frankly,” Betsy Ankney, Johnson’s campaign manager, said she told her staff at the time. “We are running our own race, using our own message. The money is there, our numbers are there.”

The data let the Johnson campaign know it was falling short of turnout goals in the state’s suburban southeast, where it made a late push to contact Republican voters reluctant to back Donald Trump. (Johnson would comfortably outrun Trump in the region.) It gave the campaign insight to put up ads in supermarket checkout lines, especially in the northern part of the state, where it could reach women. The data also told the Johnson team not to panic when Democrats ran up a big early vote in Dane County, where many Democrats (40 percent) had previously voted on Election Day. (These were simply voters voting earlier than usual.)

And the data helped the campaign decide to run a slate of positive ads late in the campaign that, after Labor Day, even Democrats acknowledge moved numbers quickly and unexpectedly.

“Ron Johnson invested in a positive message in September and October that I think was the right thing to do strategically,” said Martha McKenna, a Democratic strategist, at a panel hosted by CQ Roll Call last week. “I definitely felt like … the tried-and-true path is to turn up the negative on your opponent, and sometimes, the right thing to do is actually take a step back and begin to tell a story about yourself.”

The data belongs to the Republican Party of Wisconsin and helped Walker win three statewide races (including a 2012 recall race). It’s a file of the state’s voters that the Johnson campaign used to rate each one on their likelihood to back either Johnson or Feingold. It incorporates each person’s past voting record and was updated periodically by a poll of 4,000 people on the list.

Models like this are common in politics — every major campaign, political committee, and well-funded outside group has them. But the Johnson campaign’s version got the results right when few others did: In late October, after the final poll had been taken, it predicted Johnson would win by 1.9 points, 48.1 percent to 46.2 percent. At the time, Democrats (and some Republicans) believed Feingold would win.

Johnson won the race with 50.2 percent of the vote, compared to Feingold’s 46.8 percent.

The reason their model succeeded, Ankney said, was a decision not to weigh the data to look more like the Democratic-friendly electorate of 2012.

“It comes down to the fact that we weren’t just taking things at face value and saying, ‘OK, this is going to look like a 2012 electorate,’” she said.

“The biggest thing that nobody understood was what a likely voter was. That has been completely upended,” she added.

Johnson’s campaign managed to make him look like the outsider in the race despite running as the incumbent, an effort aided by Feingold’s own previous 18 years in the Senate.

That was a message, the campaign said, that resonated among small-town residents who had backed President Barack Obama and Democratic Senate candidate Tammy Baldwin in 2012, but who, even more than most, were fed up with the political status quo.

“That was the wave, that was the national environment,” Ankney said. “People are sick of what’s happening in Washington, D.C., and we made sure to capitalize on that.”

Inside the Johnson campaign, officials made a rallying cry out of a speech in the movie “Any Given Sunday.” The film, starring Al Pacino, features a burnt-out football coach making an impassioned pre-game speech about importance of fighting “one inch at a time.”

Members of the campaign would watch the speech in staff meetings and quote it regularly during the day, and Ankney would include a tagline of “fight for that inch” in her emails to staff and surrogates.

But the only way to find those inches, they said, was through the data they had assembled.

“Football analogies are common in politics,” said Brian Reisinger, a spokesman for the campaign. “But we couldn’t have fought for that inch if we didn’t have a data game tracking it.”