  We study the cross-correlation between maps of the unresolved $\gamma$-ray
background constructed from the 12-year data release of the Fermi Large-Area
Telescope, and the overdensity of galaxies in the redshift range $z\lesssim0.4$
as measured by the 2MASS Photometric Redshift survey and the WISE-SuperCOSMOS
photometric survey. A signal is detected at the $8-10\sigma$ level, which we
interpret in terms of both astrophysical $\gamma$-ray sources, and WIMP dark
matter decay and annihilation. The sensitivity achieved allows us to
characterise the energy and redshift dependence of the signal, and we show that
the latter is incompatible with a pure dark matter origin. We thus use our
measurement to place an upper bound on the WIMP decay rate and the annihilation
cross-section, finding constraints that are competitive with those found in
other analyses. Our analysis is based on the extraction of clean
model-independent observables that can then be used to constrain arbitrary
astrophysical and particle physics models. In this sense we produce
measurements of the $\gamma$-ray emissivity as a function of redshift and
rest-frame energy $\epsilon$, and of a quantity $F(\epsilon)$ encapsulating all
WIMP parameters relevant for dark matter decay or annihilation. We make these
measurements, together with a full account of their statistical uncertainties,
publicly available.
