We define the generalized Tanaka connection for contact Riemannian manifolds generalizing one for nondegenerate, integrable
CR
{\text {CR}}
manifolds. Then the torsion and the generalized Tanaka-Webster scalar curvature are defined properly. Furthermore, we define gauge transformations of contact Riemannian structure, and obtain an invariant under such transformations. Concerning the integral related to the invariant, we define a functional and study its first and second variational formulas. As an example, we study this functional on the unit sphere as a standard contact manifold.