If this is a new behavior, it might be worth a vet visit to make sure they don’t have a bladder infection or UTI.
If that all checks out, it’s good to note that dogs pee for a variety of reasons. It may be nervous peeing in both situations. In their mind, they think, “There are people here, I really hope they like me. You want me to sit? I hope I can do it right!”
To combat this, have people ignore your dog when they met them. If strangers are excited then they get excited or nervous and then pee. If strangers remain calm then this helps your dog increase their bladder control.
If you have guests who just can’t ignore such a cute pup, then have the strangers scatter treats on the floor as they walk in the door. If your dog is busy searching for treats then they can’t squat to pee.
Use a treat to help them with the “sit” command. Put it right at their nose and slowly lift it up and back toward their butt in an arch. Most dogs work like teeter totters, if their head goes up, their buttwill go down. As they sit, praise them calmly.
If they repeatedly step backwards instead of sitting down, then try to do the exercise against a corner cabinet or couch. If they can’t back up they will likely sit.
Calmly praise your pup when they do comply. If you get excited when they sit then they will pee. Only pet them under the chin and on the chest. This will teach your dog that standing and sitting gets love and it is hard to pee in both situations.
If your dog rolls on their belly to be pet without you asking too, stop petting them, take a couple steps back, and encourage them to stand. This will help them learn that brave behavior gets attention and nervous behavior gets ignored.