Donald Trump has opened a narrow lead in the presidential race, as voters have adopted a more positive view of his agenda and past performance and a more negative view of Kamala Harris , a new Wall Street Journal poll finds.

The national survey finds that Trump is leading Harris by 2 percentage points, 47% to 45%, compared with a Harris lead of 2 points in the Journal’s August survey on a ballot that includes third-party and independent candidates. Both leads are within the polls’ margins of error , meaning that either candidate could actually be ahead.

The survey suggests that a barrage of negative advertising in the campaign and the performance of the candidates themselves have undermined some of the positive impressions of Harris that voters developed after she replaced President Biden as the presumed and then confirmed Democratic nominee.

Views of Harris have turned more negative since August, when equal shares of voters viewed her favorably and unfavorably. Now, the unfavorable views are dominant by 8 percentage points, 53% to 45%. Moreover, voters give Harris her worst job rating as vice president in the three times the Journal has asked about it since July, with 42% approving and 54% disapproving of her performance.

By contrast, views of Trump have turned rosier. Voters recall his time as president more positively than at any point in this election cycle, with 52% approving and 48% disapproving of his performance in office—a 4-point positive job rating that contrasts with the 12-point negative rating for Harris.

Moreover, voters give Trump a solid edge in most cases when asked about the candidates’ agendas and policies. By 10 points, more voters have a favorable than unfavorable view of Trump’s economic plan for the country, while unfavorable views of Harris’s economic plan outweigh positive views by 4 points.

The poll comes as the campaigns have spent heavily on advertising to shape the images of the candidates, particularly of Harris, who is far more of an unknown figure to voters. Trump and his allies have spent more than $378 million on broadcast, cable, radio and digital ads since the party conventions ended in late August, the ad-tracking firm AdImpact reports, much of it aimed at casting her as too liberal for the nation. Harris and her allies have spent more than $625 million, much of it aimed at introducing her on more favorable terms to voters.

“Voters are finally getting to know her,” said the Republican pollster David Lee , who conducted the Journal survey with a Democrat, Michael Bocian . “The definitional period is coming to an end, and more people are unhappy with what they have learned about her than what they know about President Trump.” Lee also conducts polling for MAGA Inc., the main pro-Trump super PAC.

Bocian pointed out that voters in the survey viewed Trump more than Harris as “too extreme,” and that Harris retains a strong advantage as the candidate best able to handle abortion—an important issue to many voters. She also holds a slight lead when voters are asked who can best look out for the middle class.

“If she can succeed in making this campaign about those images and issues first and foremost, while also continuing to make the case for who’s the best fighter for the middle class, she can win a very close race,” Bocian said.

Bocian noted that the Journal’s poll earlier this month of the seven battleground states likely to determine the Electoral College winner found them all essentially tied, with Trump building a meaningful advantage only in Nevada, which like the others was within the poll’s margin of error. “All signs point to a race that’s very much up in the air, and the last two weeks will determine who votes and who wins in a close race,” Bocian said.

A number of factors could yet affect the outcome. With both campaigns racing to turn out voters who side with their candidate, the survey found that both campaigns have a motivation challenge. Some 74% of Black voters, a group that strongly favors Harris, say they will definitely vote, compared with 81% of white voters, a group on the whole that leans toward Trump.

Trump’s challenge appears to be in motivating the young men who lean toward him, and who show less voting enthusiasm than do young women, who lean toward Harris. The result in closely divided states could well come down to which of these groups turn out in the largest numbers, as well as to their ultimate choice of candidate.

Another factor: Several of the battleground states allow residents to register to vote on Election Day, among them Michigan, Wisconsin and Nevada. Voters who do so aren’t reflected in the Journal poll, which surveyed only voters who have already registered.

The survey painted a mixed portrait of Trump, with voters favoring his policies but questioning his stability and other personal qualities.

The former president has expanded his lead as the candidate best able to handle the economy , with more voters choosing him over Harris by 12 points, compared with 8 points in August. His lead over Harris as best on handling immigration has grown to 15 points, up from 7 points in August.

Trump’s advantage on handling immigration comes as he has focused heavily in his campaign appearances lately on casting illegal immigration as a danger to Americans’ personal safety and job opportunities. Some 23% of voters said immigration was the top issue motivating their choice of candidate, the largest share in Journal polls dating back a year and a half.

But in addition to being viewed as “too extreme”—a label that voters attached to Trump more than to Harris, 49% to 39%—more voters also said they saw Trump as a danger to the country, 48% to 43%.

Trump led Harris by about 10 points on which candidate had the right experience for the job and would bring needed change. But more voters said Harris, rather than Trump, had the right temperament for the office and was “mentally up for the job.” By 13 points, more voters said the word “unstable” fit Trump more than Harris.

Harris’s lead as the best candidate to handle abortion—her strongest issue—has narrowed slightly, from a 20-point advantage to 14 points.

The Wall Street Journal poll surveyed 1,500 registered voters from Oct. 19 to 22. The margin of error for the full sample is plus or minus 2.5 percentage points.

Write to Aaron Zitner at aaron.zitner@wsj.com