SAN FRANCISCO — Facebook and Twitter announced plans on Thursday to increase transparency of political campaign ads, changes aimed at preventing foreign manipulation of the coming midterm elections.

Facebook said it would begin including a “paid for” label on the top of any political ads in the United States. Clicking on the label will take people to a page where they can view the cost of the ad and the demographic breakdown of the audience that viewed the ad.

Facebook also promised to keep an archive of all political ads for the next seven years, or through a full congressional election cycle. The ads are available to Facebook members at facebook.com/politicalcontentads.

Twitter said it planned to restrict who could run political ads on its service, requiring those running political ads for federal elections to identity themselves and certify that they are in the United States. Foreign nationals will not be able to target political ads to people in the United States, Twitter said.