fields as sources of gravity

Imagine a situation where the gravitational field is sourced by a complex scalar field: why do gauge transformations not matter? why is this the case for the electromagnetic field?