Republican presidential candidate Donald Trump addresses members of the National Rifle Association at their annual meeting in Louisville, Ky., May 20, 2016. (John Sommers II/Reuters)

President Trump's appearance Friday before a convention of the National Rifle Association in Atlanta underscores the powerful role the gun rights group played in his election, providing critical support in battleground states and helping him score a crucial win in Pennsylvania, a new analysis shows.

The NRA has been a muscular force in American politics for decades. But last year it spent more for Trump than any outside group and began its efforts earlier than in any other presidential cycle.

A comparison by The Washington Post of ad spending between 2012 and 2016 found that the gun rights organization spent more than three times as much money to assist Trump as it spent backing GOP nominee Mitt Romney in 2012, airing 4.5 times as many individual ads.

“The NRA provided crucial air cover for Trump in pivotal states at a critical time,” said Ken Goldstein, a University of San Francisco political scientist who studies campaign advertising. The millions spent by the NRA was especially important, Goldstein said, because the Trump campaign and allied super PACs spent relatively little.

“The NRA saw a need and an opportunity,” he said. And the big bet paid off.

Trump, the first sitting president to address the NRA since Ronald Reagan did so, pledged during the campaign to “save our Second Amendment” and nominate judges who would support expansive gun rights.

Already, the NRA has cheered Trump’s first Supreme Court pick, Neil M. Gorsuch, as well as his choice of Jeff Sessions as attorney general and Ryan Zinke as interior secretary. Both Cabinet members have views largely in sync with the NRA’s.

The gun rights organization also got an early win under Trump when he signed legislation repealing an Obama administration regulation that sought to block gun purchases by certain people who are unable to administer their own financial affairs.

In the months ahead, the NRA will be looking for Trump to put his weight behind a bill in Congress that would make concealed-carry permits valid in states other than those in which they were issued. Trump endorsed the concept during the campaign, likening it to the portability of driver's licenses.

Also high on the NRA's agenda is the Hearing Protection Act, which would remove federal registration and identification requirements for those seeking gun silencers. That measure has been touted by the president's son, Donald Trump Jr., an avid hunter.

Part of the NRA’s motivation in backing Trump was that it viewed Democrat Hillary Clinton as particularly hostile to its agenda.

But Trump's kinship with NRA members — many of whom are the white, working-class voters he successfully wooed in defeating Clinton — extended beyond gun issues.

“They were a big part of the need-an-outsider coalition,” said Barry Bennett, a Trump political adviser during the general election campaign. “I think it had as much to do with anger at Washington than any particular gun issue.”

The Post analysis, based on television market data supplied by Kantar Media, found that the NRA paid $4.8 million on 3,106 individual spots in the 2012 cycle, going on air for the first time Oct. 8, 2012.

Last year, by comparison, the pro-gun group spent $16.1 million on 14,129 individual spots starting in late June.

The NRA was active in several battleground states, including North Carolina and Ohio. But the surge in NRA presidential ad spending was particularly visible in Pennsylvania, a state where the organization spent nothing on TV ads in 2012. In 2016, it spent $2.3 million to air 2,378 spots in five markets

One of those markets was Wilkes-Barre, in Luzerne County, which Obama won in 2012 with 51.7 percent of the vote compared with Romney’s 46.8 percent. Four years later, Trump trounced Clinton in Luzerne County, winning 57.9 percent of the vote to Clinton’s 38.6 percent. The NRA aired nothing in the market in 2012, but four years later the gun rights group paid to run ads nearly 650 times in the Wilkes-Barre media market.

“It clearly had an effect,” said Kevin Washo, a central Pennsylvania native who is a former executive director of the state’s Democratic Party.

He noted that the Clinton campaign invested heavily in advertising and visits to the state but concentrated activity in the Philadelphia and Pittsburgh markets. "Trump allies needed to respond on the airwaves, and the NRA knew what they had to do" in critical, less urban regions of the state, Washo said.

“The NRA’s leaders invested big, and now they’re expecting a dividend,” said John Feinblatt, president of the group Everytown for Gun Safety, which works to control gun-related violence, during a conference call on the eve of Trump’s Atlanta appearance.

Trump returns to Pennsylvania on Saturday, holding a rally in Harrisburg to mark 100 days of his presidency.

NRA spending rose significantly also in Ohio, a perennial presidential battleground that also flipped from Obama in 2012 to Trump in 2016, with NRA television ad spending rising to $5.6 million last year, more than 3.5 times what the organization spent four years earlier.

It is impossible to gauge precisely the impact of this spending. But Democratic Party campaign officials credit the NRA with successfully targeting the same group of angry white voters who carried Trump to his surprise win over Hillary Clinton. The oft-repeated NRA television ads emphasized starkly negative images and messages about Clinton, portraying her as a threat to gun rights and national security.

One ad shown more than 2,775 times in Pennsylvania and other states featured Marine Corps veteran Mark Geist against the backdrop of a military cemetery. Over dramatic music with a staccato drumbeat, Geist says: “Hillary as President? No thanks. I served in Benghazi. My friends didn’t make it. They did their part. Do yours.” The ad closes with news footage of a desecrated American flag and a building in flames, with the words “Stop Hillary” stamped across the screen.

Another ad that ran repeatedly in Pennsylvania, North Carolina and Ohio showed a woman in bed at night as a burglar breaks into her home. A narrator says, “Don’t let Hillary leave you protected with nothing but a phone.”

The NRA has long been an important force in presidential elections. But 2016 was different in part because other outside GOP groups, such as American Crossroads GPS, declined to back Trump. The leading pro-Trump super PAC spent $20.3 million, according to the Center for Responsive Politics, a third less than the NRA spent overall.

Trump’s appearance at the gun group’s massive convention will recall the triumphant moment a year ago when the NRA endorsed him earlier than it had backed any other presidential candidate.

He told the audience then that “Crooked Hillary is the most anti-gun, anti-Second Amendment candidate ever to run for office.”

NRA support for Trump was particularly important over the summer, because there was virtually no other advertising backing the real estate mogul. And the support did not dwindle after the election.

On Inauguration Day, the organization distributed a news release saying that “gun owners across the nation breathed a sigh of relief as Donald J. Trump was sworn in Friday morning as the 45th president of the United States.”

Alice Crites contributed to this report.