Expanding foam will stick to glass.
The trick is to make the first layer as small a bead as possible. In my experience rows of 1/4" beads allows the best contact to glass. Often when you go full flow the expanding foam pulls away from the glass while curing.
You will always get some foam pulling away, but with a thin layer first it's easy to build on with subsequent layers.
I do the same thing when adding foam to silicone.
Additionally I would let the foam cure a full 24 hrs before carving or adding a second layer and make sure the glass is clean! I use some solvent before foaming to ensure there are no oils present.