Teaching your dog to bark at a stranger requires patient and consistent training.
Teach your dog to speak.
Before you can direct your dog when to bark, it's important that he learns the "speak" command.
When your dog gets excited, encourage him to bark by saying the word "speak" or another word of your choice