ga.js to Analytics.js Converter
Convert legacy Google Analytics ga.js (_gaq.push) code to modern Analytics.js (analytics.js) syntax. Supports events, ecommerce, custom vars, and more.
About
Google deprecated ga.js (Classic Analytics) in favor of analytics.js (Universal Analytics) in 2014. Sites still running _gaq.push calls risk silent data loss: the legacy library may stop receiving updates at any time, custom dimensions replace custom variables with a hard limit of 5 slots vs. 20, and cross-domain tracking semantics changed entirely. Manual migration is error-prone because argument order differs between the two APIs. For example, _trackEvent takes 4 positional strings while ga('send','event',...) uses named fields. This converter parses each _gaq.push call, identifies the method signature, remaps arguments to the Universal Analytics equivalent, and outputs clean ga() calls. It handles the snippet replacement, ecommerce plugin loading, campaign timeouts (converted from milliseconds to seconds), sample rates, and domain configuration. It does not guess intent. Lines it cannot map are flagged with a comment so you can review them manually.
Formulas
The converter applies a deterministic mapping function for each ga.js call. Given an input statement S containing a method call m with arguments a1, a2, โฆ, an, the output is:
where Map is a lookup dictionary of 26 method signatures. Some methods require argument reordering. For _trackEvent:
For time-based settings, a unit conversion is applied:
Where m is the ga.js method name string, ai are the original positional arguments, O is the output Analytics.js statement, cat = event category, act = event action, lbl = event label, val = numeric event value. Lines that contain no recognized ga.js pattern pass through unmodified. Unmappable methods are preserved as comments prefixed with /* UNMAPPED */.
Reference Data
| ga.js Method | Analytics.js Equivalent | Notes |
|---|---|---|
| _setAccount | ga('create', id, 'auto') | Tracker creation |
| _trackPageview | ga('send', 'pageview') | Optional page path argument preserved |
| _trackEvent | ga('send', "event", ...) | Category, Action, Label, Value mapped positionally |
| _setCustomVar | ga('set', "dimension" + index, value) | Slot index becomes dimension number; scope dropped |
| _addTrans | ga('ecommerce:addTransaction', {...}) | Requires ecommerce plugin |
| _addItem | ga('ecommerce:addItem', {...}) | Requires ecommerce plugin |
| _trackTrans | ga('ecommerce:send') | Dispatches queued transactions |
| _setDomainName | ga('create', id, {cookieDomain: ...}) | Merged into create call |
| _setAllowLinker | ga('require', 'linker') | Requires linker plugin |
| _link | ga('linker:decorate', ...) | Cross-domain link decoration |
| _setAllowHash | // removed | Hash-based cookies no longer used |
| _setCookiePath | ga('create', id, {cookiePath: ...}) | Merged into create options |
| _setSampleRate | ga('create', id, {sampleRate: ...}) | Value is percentage (0 - 100) |
| _setSiteSpeedSampleRate | ga('create', id, {siteSpeedSampleRate: ...}) | Default is 1% |
| _setCampaignCookieTimeout | ga('create', id, {campaignTimeout: ...}) | Converted from ms to seconds |
| _setVisitorCookieTimeout | // removed | No direct equivalent; use cookie expiration |
| _trackSocial | ga('send', "social", ...) | Network, Action, Target mapped |
| _trackTiming | ga('send', "timing", ...) | Category, Variable, Value, Label |
| _setAllowAnchor | ga('create', id, {allowAnchor: ...}) | Boolean preserved |
| _anonymizeIp | ga('set', "anonymizeIp", true) | GDPR-relevant setting |
| _require | ga('require', pluginName) | Plugin loading |
| _get | // not directly mappable | Use ga(function(t){t.get(...)}) |
| _deleteCustomVar | ga('set', "dimension" + index, '') | Set to empty string |
| _setSessionCookieTimeout | ga('create', id, {sessionTimeout: ...}) | Converted from ms to minutes |
| _addOrganic | // removed | Organic source config no longer supported |
| _addIgnoredOrganic | // removed | Branded terms exclusion handled in GA UI |
| _addIgnoredRef | // removed | Referral exclusions handled in GA UI |