Redirect Notice

Redirect Notice

The page you requested has been relocated to Player Docs.

Using Global Site Tag (gtag.js) with Universal and Google Analytics 4

Using Global Site Tag (gtag)

If using Global Site Tag as the tagging framework for Universal Analytics and Google Analytics 4 you can instruct AMP to report data throughout gtag.js by setting the mode property as 'gtag'

The following sample shows how to turn gtag in googleanalytics plugin for AMP.

Google Analytics 4

Using gtag as data collector for Google Analytics 4

var config = {
  plugins: {
    googleanalytics: {
      resources: [
        {
          src: "https://www.googletagmanager.com/gtag/js?id=G-FC9DG1B2JV",
          type: "text/javascript",
          async: true,
        },
        {
          src: "#{paths.plugins}googleanalytics/Googleanalytics.js",
          type: "text/javascript",
          async: true,
        },
      ],
      trackingId: "G-FC9DG1B2JV",
      mode: "gtag",
      events: {
        video: [
          {
            type: "started",
            data: {
              metric1: "viewDuration",
              dimension1: "#{media.title}",
              metric2: "#{media.duration}",
            },
          },
          {
            type: "ended",
            data: {
              metric1: "viewDuration",
              dimension1: "#{media.title}",
              metric2: "#{media.duration}",
            },
          },
        ],
        ads: [
          {
            type: "adstarted",
            data: {
              dimension3: "#{ads.currentAd.title}",
              metric3: "#{ads.currentAd.duration}",
            },
          },
          {
            type: "adended",
            data: {
              dimension3: "#{ads.currentAd.title}",
              metric3: "#{ads.currentAd.duration}",
            }
          }
        ]
      }
    }
  }
};

Universal Analytics

Using gtag as data collector for Universal Analytics

var config = {
  plugins: {
    googleanalytics: {
      resources: [
        {src: "https://www.googletagmanager.com/gtag/js?id=UA-82788715-3", type: "text/javascript", async: true},
        {src: "#{paths.plugins}googleanalytics/Googleanalytics.js", type: "text/javascript", async: true}
      ],
      trackingId: "UA-82788715-3",
      mode: "gtag",
      events: {
        video: [{
          type: "started",
          data: {
            eventCategory: "video",
            eventAction: "videoStart",
            customDimension : {
              metric1: "viewDuration",
              dimension1: "#{media.title}",
              metric2: "#{media.duration}"
            }
          }
        }, {
          type: "ended",
          data: {
            eventCategory: "video",
            eventAction: "videoEnded",
            nonInteraction: true,
            customDimension : {
              metric1: "viewDuration",
              dimension1: "#{media.title}",
              metric2: "#{media.duration}"
            }
          }
        }],
        ads: [{
          type: "started",
          data: {
            eventCategory: "ad",
            eventAction: "adStarted",
            nonInteraction: true
          }
        }, {
          type: "ended",
          data: {
            eventCategory: "ad",
            eventAction: "adsEnded"
          }
        }
        ]
      }
    }
  }
};