We present a multiwavelength study of the flat-spectrum radio quasar CTA 102 during 2013-2017. We use radio-to-optical data obtained by the Whole Earth Blazar Telescope, 15 GHz data from the Owens Valley Radio Observatory, 91 and 103 GHz data from the Atacama Large Millimeter Array, near-infrared data from the Rapid Eye Monitor telescope, as well as data from the Swift (optical-UV and X-rays) and Fermi ($gamma$ rays) satellites to study flux and spectral variability and the correlation between flux changes at different wavelengths. Unprecedented $gamma$-ray flaring activity was observed during 2016 November-2017 February, with four major outbursts. A peak flux of (2158 $pm$ 63)$times$10$^{-8}$ ph cm$^{-2}$ s$^{-1}$, corresponding to a luminosity of (2.2 $pm$ 0.1)$times$10$^{50}$ erg s$^{-1}$, was reached on 2016 December 28. These four $gamma$-ray outbursts have corresponding events in the near-infrared, optical, and UV bands, with the peaks observed at the same time. A general agreement between X-ray and $gamma$-ray activity is found. The $gamma$-ray flux variations show a general, strong correlation with the optical ones with no time lag between the two bands and a comparable variability amplitude. This $gamma$-ray/optical relationship is in agreement with the geometrical model that has successfully explained the low-energy flux and spectral behaviour, suggesting that the long-term flux variations are mainly due to changes in the Doppler factor produced by variations of the viewing angle of the emitting regions. The difference in behaviour between radio and higher energy emission would be ascribed to different viewing angles of the jet regions producing their emission.