<<xAct`xTras`

$PrePrint=ScreenDollarIndices;

DefManifold[M,4,{a,b,c,d,f}]

DefMetric[-1,g[-a,-b],cd]

DefTensor[A[a],M]

DefScalarFunction[G2,PrintAs->"(Subscript[G, 2])"]

DefScalarFunction[G3,PrintAs->"(Subscript[G, 3])"]

DefScalarFunction[G4,PrintAs->"(Subscript[G, 4])"]

X[]:=Scalar[-1/2g[-a,-b]A[a]A[b]]

Variations with respect to the vector field A:

?VarD

VarD[A[a],cd][G2[X[]]]

VarD[A[a],cd][G3[X[]]cd[-a][A[a]]]//ToCanonical

VarD[A[a],cd][G4[X[]]RicciScalarcd[]+G4'[Scalar[cd[-a][A[a]]]^2-cd[-b][A[c]]cd[-c][A[b]]]]//NoScalar//ToCanonical

Variations with respect to the metric:

?VarL

VarL[g[-a,-b],cd][G2[X[]]]

VarL[g[-a,-b],cd][G3[X[]]cd[-a][A[a]]]//ToCanonical

VarL[g[-a,-b],cd][G4[X[]]RicciScalarcd[]+G4'[Scalar[cd[-a][A[a]]]^2-cd[-b][A[c]]cd[-c][A[b]]]]//NoScalar//ToCanonical