This is a tough one. Your dog has probably been conducting this behavior for quite a while, uninterrupted. Further he believes his antics are working. A person or dog approaches, he starts running the fence and barking at them, the person or dog moves more quickly past the fence line, your dog continues to bark, and eventually, the person or dog disappears in the distance. Your dog has successfully scared them away with his antics. How in the world do you counter-condition your pup against such strong reinforcing behavior? Like I said, it won’t be easy, and will take many blocks of your time to succeed.
First things first, your dog must be strong on his basic obedience training. He needs a solid sit, down, touch (a version of come), stay, place, and especially “leave it”. Please review my past blogs if you need help on teaching your dog the above commands. Assuming your dog is solid on these commands, you will need to spend a good half-hour a day outside with him.
You will need to watch for whenever your dog’s ears perk up, which is usually often enough. When this happens, you should immediately say “leave it”, walk away from the fence line 10 feet, saying the command “away” at the same time. Oh, and you should have high-value treats in your hand all the time during these exercises. Once you are 10 feet away (or so) from the fence, use your clicker or say the marker word “yes!” and reward your pup. Rinse and repeat as much as you can in a half hour.
You will want to do this in different areas of the fence line and do it randomly. If some time passes and there is no activity which perks his ears up, run some basic training routines with him to keep him sharp. It is very important that during this training process you issue your commands before he goes into a full-blown, amped up fence run complete with barking. If this does happen, your pup will not be reachable until the stimulus has passed. Just pretend nothing happened and start over with the program described above.
Eventually, perhaps after several weeks or several months (depending on your dog), your pup will see a stimulus approach, and calmly walk away from the fence line looking for a treat. Make sure you are there to reward him some percentage of the time for at least a few more months. After that, I would every so often reward him for making the right choices so he thinks in his head, “maybe my person will reward me this time.”