Z tego przewodnika dowiesz się, jak przenieść dotychczasową implementację analytics.js
Universal Analytics do gtag.js
.
Przegląd
Kod analytics.js ma 2 główne mechanizmy wysyłania danych do Google Analytics:
-
Lokalizatory określają, którą właściwość mierzysz.
-
Typy działań określają, jaki rodzaj interakcji chcesz mierzyć.
W usłudze gtag.js
właściwości określa się za pomocą polecenia config
lub jako parametru polecenia.
W przeciwieństwie do tagu analytics.js tag gtag.js nie używa skryptów śledzenia do wysyłania danych do Google Analytics. Wysyła dane do usług w Google Analytics identyfikowanych za pomocą identyfikatorów określonych w poleceniu config
. Nazwy zdarzeń podawane w tagu gtag.js określają typy danych wysyłanych do Google Analytics.
Aby przejść z tagu analytics.js na tag gtag.js, w przypadku każdej strony internetowej w swojej witrynie wykonaj te czynności:
Zastąp kod analytics.js kodem gtag.js równoważnym do:
Zastąp fragment kodu analytics.js fragmentem kodu gtag.js.
Zastąp na swojej stronie fragment kodu analytics.js:
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'TAG_ID', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'TAG_ID');
</script>
Pomiar wyświetleń strony
Tag analytics.js wykorzystuje moduły śledzenia do wysyłania informacji o odsłonach do Google Analytics. Tag śledzenia ma identyfikator pomiaru powiązany z usługą w Google Analytics. Tag gtag.js wysyła odsłony do usługi w Google Analytics identyfikowanej za pomocą parametru TAG_ID
określonego w poleceniu config
.
Mierz odsłony za pomocą domyślnego narzędzia do śledzenia
Usuń ten kod analytics.js, który korzysta z domyślnego modułu do śledzenia wyświetleń stron w Google Analytics send
:
// Creates the default tracker.
ga('create', 'TAG_ID', 'auto');
// Uses the default tracker to send a pageview to the
// Google Analytics property with tag ID of 'TAG_ID'.
ga('send', 'pageview');
Ten kod we fragmencie kodu gtag.js automatycznie wysyła do usługi w Google Analytics odsłonę o identyfikatorze tagu TAG_ID
:
gtag('config', 'TAG_ID');
Mierzenie odsłon za pomocą określonych skryptów śledzenia
Zastąp ten kod analytics.js, który korzysta z podanego tagu śledzenia do wysyłania odsłon do Google Analytics:
ga('create', 'TAG_ID', 'auto', 'trackerName');
ga('trackerName.send', 'pageview');
tym poleceniem event
tagu gtag.js:
gtag('event', 'page_view', { 'send_to': 'TAG_ID' });
Mierzenie zdarzeń
Jak już wspomnieliśmy, analytics.js używa tagów śledzenia do wysyłania zdarzeń do Google Analytics. Moduł do śledzenia ma identyfikator śledzenia usługi Google Analytics. Z kolei tag gtag.js wysyła zdarzenia do usługi Google Analytics identyfikowanej przez parametr TAG_ID
określony w poleceniu config
.
Mierz zdarzenia za pomocą domyślnego trackera
Zastąp ten kod analytics.js, który korzysta z domyślnego tagu śledzenia, jako zdarzenia send
w Google Analytics:
ga('create', 'TAG_ID', 'auto');
ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
tym poleceniem gtag.js event
:
gtag('event', eventName, eventParameters);
gdzie eventName
to nazwa zdarzenia, które chcesz zarejestrować.
Przykład:
Kod analytics.js:
// Creates the default tracker.
ga('create', 'TAG_ID', 'auto');
// Uses the default tracker to send the event to the
// Google Analytics property with a tag ID of `TAG_ID`.
ga('send', 'event', 'Videos', 'play', 'Fall Campaign');
gtag.js
// Sends the event to the Google Analytics property with a
// tag ID of `TAG_ID` set by the config command in
// the gtag.js snippet.
gtag('event', 'play', {
'event_category': 'Videos',
'event_label': 'Fall Campaign'
});
Mierz zdarzenia za pomocą określonych skryptów śledzenia
Zastąp ten kod analytics.js, który korzysta z podanego tagu śledzenia do wysyłania zdarzeń do Google Analytics:
ga('create', 'TAG_ID', 'auto', 'trackerName');
ga('trackerName.send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
tym poleceniem event
tagu gtag.js:
gtag('event', eventName, {
'send_to': 'TAG_ID',
'parameter1': 'value1',
'parameter2': 'value2',
// ...
});
Przykład:
Kod analytics.js:
// Creates a tracker named <b>clientTracker</b>.
ga('create', 'TAG_ID', 'auto', 'clientTracker');
// Uses tracker clientTracker to send the event to the
// Google Analytics property with a tag ID of TAG_ID.
ga('clientTracker.send', 'event', 'Videos', 'play', 'Fall Campaign');
gtag.js
// Send the event to the Google Analytics property
// with a tag ID of 'TAG_ID'.
gtag('event', 'play', {
'send_to': 'TAG_ID',
'event_category': 'Videos',
'event_label': 'Fall Campaign'
});
Wysyłaj wymiary i dane niestandardowe
Zastąp na swoich stronach każde polecenie send
tagu analytics.js, które wysyła wymiary niestandardowe do Google Analytics:
ga('send', 'hitType', { 'dimension<Index>': 'dimension_value'});
z tym kodem gtag.js:
gtag('config', 'TAG_ID', {
'custom_map': {'dimension<Index>': 'dimension_name'}
});
gtag('event', 'any_event_name', {'dimension_name': 'dimension_value'});
Zastąp TAG_ID
własnym identyfikatorem Analytics.
Zastąp na swoich stronach polecenia send
za pomocą biblioteki analytics.js, które wysyłają dane niestandardowe do Google Analytics:
ga('send', 'hitType', { 'metric<Index>': 'metric_value'});
z tym kodem gtag.js:
gtag('config', 'TAG_ID', {
'custom_map': {'metric<Index>': 'metric_name'}
});
gtag('event', 'any_event_name', {'metric_name': 'metric_value'});
Zastąp TAG_ID
identyfikatorem tagu.
Mierzenie czasu działań użytkownika
Zastąp na swoich stronach polecenia send
za pomocą biblioteki analytics.js, które śledzą czasy działań użytkowników:
ga('send', 'timing', 'timingCategory', 'timingVar', timingValue, 'timingLabel');
tym poleceniem gtag.js event
:
gtag('event', 'timing_complete', {
'name': 'timingVar',
'value': timingValue,
'event_category': 'timingCategory',
'event_label': 'timingLabel'
});
Mierz wyjątki
Zastąp na swoich stronach polecenia send
za pomocą biblioteki analytics.js, które śledzą wyjątki:
ga('send', 'exception', {
'exDescription': 'error_message',
'exFatal': false // set to true if the exception is fatal
});
za pomocą tego polecenia gtag.js event
:
gtag('event', 'exception', {
'description': 'error_message',
'fatal': false // set to true if the exception is fatal
});
Zmapuj pola analytics.js na parametry gtag.js
Tabele poniżej mapują pola analytics.js na odpowiednie parametry gtag.js.
Wydarzenia
pole analytics.js | Parametr gtag.js |
---|---|
eventAction |
event_action |
eventCategory |
event_category |
eventLabel |
event_label |
eventValue |
value |
Wymiary i dane niestandardowe
pole analytics.js | Parametr gtag.js |
---|---|
dimension<Index> |
dimension<Index> |
metric<Index> |
metric<Index> |
gdzie <Index>
to nieujemna liczba całkowita określająca indeks niestandardowego wymiaru lub rodzaju danych.
Czas działań użytkownika
pole analytics.js | Parametr gtag.js |
---|---|
timingCategory |
event_category |
timingLabel |
event_label |
timingValue |
value |
timingVar |
name |
Pomiar wyjątków
pole analytics.js | Parametr gtag.js |
---|---|
exDescription |
description |
exFatal |
fatal |
Dane dotyczące działań ulepszonego e-commerce
pole analytics.js | Parametr gtag.js |
---|---|
id |
transaction_id |
affiliation |
affiliation |
revenue |
value |
tax |
tax |
shipping |
shipping |
coupon |
coupon |
list |
list_name |
step |
checkout_step |
option |
checkout_option |
Dane promocji
pole analytics.js | Parametr gtag.js |
---|---|
creative |
creative_name |
position (wyświetlenie, produkt) |
list_position |
position (promocja) |
creative_slot |
Działania związane z produktem i promocją
pole analytics.js | Zdarzenie gtag.js |
---|---|
add |
add_to_cart |
checkout (pierwszy krok) |
begin_checkout |
checkout (każdy kolejny krok) |
checkout_progress |
checkout_option |
set_checkout_option |
click |
select_content (bez promocji) |
detail |
view_item |
promo_click |
select_content (z promocjami) |
purchase |
purchase |
refund |
refund |
remove |
remove_from_cart |
Client-ID i User-ID.
pole analytics.js | Parametr gtag.js |
---|---|
clientId |
client_id |
userId |
user_id |