What does God do? His first attempt to get Balaam's attention fails—not with the donkey, but with Balaam. The man is totally oblivious to what is going on. So God narrows him in or hedges him in. The path that Balaam was taking led between two hedges or walls. There was enough room, however, for the donkey to turn aside, which is what she did. She turned away, but in doing so, Balaam's foot became crushed against the wall, causing him pain. Perhaps God thought that a little pain would help him come to his senses.
However, Balaam does not think about God at all. He thinks, "You stupid donkey! Why did you do that to me?" He does not say anything at this point but beats the poor donkey. His injury does not cause him to consider at all that God may be trying to get his attention. It never comes to mind that God may be telling him something. He takes all his pain and rage out on this innocent donkey, which was only trying to obey God.
Think of the donkey in terms of this passage:
But my eyes are upon You, O GOD the Lord; in You I take refuge; do not leave my soul destitute. Keep me from the snares they have laid for me, and from the traps of the workers of iniquity. Let the wicked fall into their own nets, while I escape safely. (Psalm 141:8-10)
The donkey who saw God would have avoided the trap and escaped, if it were not for Balaam controlling her. He made her go back into the trap—and on to his own ruin.