Pledging allegiance  to flag and/or person was in use long before the USA. It goes back into the dark ages of history.

There were so many small, kings, lords, even churches and all had their own flags.

In the days of knights ect their were even household flags.