As he's writing, every time  he reaches G-d's name, he immerses, returns and  writes G-d's name and says that he's writing this for the  sake of the holiness of the tefillin, just like all the  necessary laws. 