On this page

Migrate from Google Analytics

This guide describes the options for migrating from Google Analytics (GA4) to Amplitude. Migration requires adjusting your tracking implementation to align with Amplitude's event structure and capabilities.

Amplitude offers two migration methods:

  1. A low-code Google Analytics event forwarding plugin
  2. A migration guide that highlights the differences between Google Analytics and Amplitude tracking.

Import historical GA4 data

You can import your historical Google Analytics 4 data into Amplitude through BigQuery. For setup instructions, refer to the Google Analytics 4 Import documentation.

Forward events from Google Analytics

For less complex implementations, Amplitude offers a low-code migration tool: the Google Analytics Event Forwarder.

How the Google Analytics events forwarder works

The forwarder is a plugin to the Amplitude Browser SDK. After you install the plugin on your application, the plugin listens for the events that Google Analytics (GA4) tracks. For each event sent to Google Analytics (GA4), the plugin sends a corresponding event to Amplitude.

Initialize Amplitude with Google Analytics events forwarder

Add the Amplitude SDK with Google Analytics events forwarder snippet before your Google Tag snippet. Placement before the Google Tag snippet ensures that Amplitude can receive all events you collect with Google Analytics.

plaintext
<script type="text/javascript">
  !function(){"use strict";!function(e,t){var r=e.amplitude||{_q:[],_iq:{}};if(r.invoked)e.console&&console.error&&console.error("Amplitude snippet has been loaded.");else{var n=function(e,t){e.prototype[t]=function(){return this._q.push({name:t,args:Array.prototype.slice.call(arguments,0)}),this}},s=function(e,t,r){return function(n){e._q.push({name:t,args:Array.prototype.slice.call(r,0),resolve:n})}},o=function(e,t,r){e[t]=function(){if(r)return{promise:new Promise(s(e,t,Array.prototype.slice.call(arguments)))}}},i=function(e){for(var t=0;t<m.length;t++)o(e,m[t],!1);for(var r=0;r<y.length;r++)o(e,y[r],!0)};r.invoked=!0;var a=t.createElement("script");a.type="text/javascript",a.crossOrigin="anonymous",a.src="https://cdn.amplitude.com/libs/plugin-ga-events-forwarder-browser-0.4.1-min.js.gz",
  a.onload=function(){e.gaEventsForwarder&&e.gaEventsForwarder.plugin&&e.amplitude.add(e.gaEventsForwarder.plugin())};var c=t.createElement("script");c.type="text/javascript",c.integrity="sha384-HpnlFSsUOQTaqmMKb6/PqZKVOBEpRji3JNLr81x6XElQ4bkquzRyG/F8rY8IDMuw",c.crossOrigin="anonymous",c.async=!0,c.src="https://cdn.amplitude.com/libs/analytics-browser-2.2.1-min.js.gz",c.onload=function(){e.amplitude.runQueuedFunctions||console.log("[Amplitude] Error: could not load SDK")};var u=t.getElementsByTagName("script")[0];u.parentNode.insertBefore(a,u),u.parentNode.insertBefore(c,u);for(var p=function(){return this._q=[],this},d=["add","append","clearAll","prepend","set","setOnce","unset","preInsert","postInsert","remove","getUserProperties"],l=0;l<d.length;l++)n(p,d[l]);r.Identify=p;for(var g=function(){return this._q=[],this},v=["getEventProperties","setProductId","setQuantity","setPrice","setRevenue","setRevenueType","setEventProperties"],f=0;f<v.length;f++)n(g,v[f]);r.Revenue=g;var m=["getDeviceId","setDeviceId","getSessionId","setSessionId","getUserId","setUserId","setOptOut","setTransport","reset","extendSession"],y=["init","add","remove","track","logEvent","identify","groupIdentify","setGroup","revenue","flush"];i(r),r.createInstance=function(e){return r._iq[e]={_q:[]},i(r._iq[e]),r._iq[e]},e.amplitude=r}}(window,document)}();

  amplitude.init('YOUR_API_KEY');
</script>

Replace 'YOUR_API_KEY' with your Amplitude API key, which you can find in your Amplitude account.

The Google Tag appears on every page of your website, immediately after the <head> element. The Google Tag for your account looks like the snippet below.

html
    <!-- Google tag (gtag.js) -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
    <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'G-XXXXXXXXXX');
    </script>

After you add both the Google Analytics and Amplitude snippets, Google Analytics events appear in Amplitude without extra instrumentation.

Event names and properties forwarded from Google Analytics retain the naming convention set in Google Analytics. For example, if you store the city in a custom dimension in Google Analytics, that event property may appear as ua_dimension_14 in Amplitude.

Amplitude also tracks the Measurement ID from Google Analytics to help you identify where an event originates.

For events that both Google Analytics and Amplitude track by default, like page_view, the plugin uses the Amplitude default to avoid duplication.

Replace Google Analytics

For more customized implementations, replace Google Analytics with Amplitude and adjust your tracking so events track correctly with Amplitude.

Initialize Amplitude

In your application's code, replace the Google Analytics (GA4) initialization script with the Amplitude initialization script.

Google Analytics script:

html
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'G-XXXXXXXXXX');
</script>

Copy the Amplitude initialization script below, or refer to GitHub for more installation options.

plaintext
<script type="text/javascript">
  !function(){"use strict";!function(e,t){var r=e.amplitude||{_q:[],_iq:{}};if(r.invoked)e.console&&console.error&&console.error("Amplitude snippet has been loaded.");else{var n=function(e,t){e.prototype[t]=function(){return this._q.push({name:t,args:Array.prototype.slice.call(arguments,0)}),this}},s=function(e,t,r){return function(n){e._q.push({name:t,args:Array.prototype.slice.call(r,0),resolve:n})}},o=function(e,t,r){e._q.push({name:t,args:Array.prototype.slice.call(r,0)})},i=function(e,t,r){e[t]=function(){if(r)return{promise:new Promise(s(e,t,Array.prototype.slice.call(arguments)))};o(e,t,Array.prototype.slice.call(arguments))}},a=function(e){for(var t=0;t<m.length;t++)i(e,m[t],!1);for(var r=0;r<g.length;r++)i(e,g[r],!0)};r.invoked=!0;var c=t.createElement("script");c.type="text/javascript",c.integrity="sha384-Chi7fRnlI3Vmej27YiXRbwAkES7Aor2707Qn/cpfhyw4lYue9vH/SOdlrPSFGPL/",c.crossOrigin="anonymous",c.async=!0,c.src="https://cdn.amplitude.com/libs/analytics-browser-2.3.2-min.js.gz",c.onload=function(){e.amplitude.runQueuedFunctions||console.log("[Amplitude] Error: could not load SDK")};
  var u=t.getElementsByTagName("script")[0];u.parentNode.insertBefore(c,u);for(var l=function(){return this._q=[],this},p=["add","append","clearAll","prepend","set","setOnce","unset","preInsert","postInsert","remove","getUserProperties"],d=0;d<p.length;d++)n(l,p[d]);r.Identify=l;for(var f=function(){return this._q=[],this},v=["getEventProperties","setProductId","setQuantity","setPrice","setRevenue","setRevenueType","setEventProperties"],y=0;y<v.length;y++)n(f,v[y]);r.Revenue=f;var m=["getDeviceId","setDeviceId","getSessionId","setSessionId","getUserId","setUserId","setOptOut","setTransport","reset","extendSession"],g=["init","add","remove","track","logEvent","identify","groupIdentify","setGroup","revenue","flush"];a(r),r.createInstance=function(e){return r._iq[e]={_q:[]},a(r._iq[e]),r._iq[e]},e.amplitude=r}}(window,document)}();

  amplitude.init("<YOUR_API_KEY>");
</script>

Track events

Amplitude's tracking function requires different parameters than Google Analytics (GA4). Compare the tracking functions in the code snippets below.

Google Analytics (GA4)

js
gtag('event', 'event_name', {
  param1: 'value1',
  param2: 'value2',
});

Amplitude

Refer to Tracking an event for more details.

js
amplitude.track('event_name, {
  param1: 'value1',
  param2: 'value2',
})

Set a user ID

Google Analytics (GA4)

js
gtag('config', 'GA_MEASUREMENT_ID', {
    user_id: 'USER_ID',
});

Amplitude

Refer to Custom user ID for more details.

js
amplitude.setUserId('USER_ID');

Set user properties

Google Analytics (GA4)

js
gtag('set', 'user_properties', {
    property1: 'value1',
    property2: 'value2',
});

Amplitude

Refer to User properties for more details.

js
amplitude.identify(
  new amplitude.Identify()
    .set('property1', 'value1')
    .set('property2', 'value2'),
);

For more information, refer to the Amplitude Browser SDK documentation.

Was this helpful?