# Cordova Google Analytics プラグイン

この Cordova プラグインは、Google のユニバーサルアナリティクス SDK に接続するために使用されます。

* Repo: <https://github.com/danwilson/google-analytics-plugin>
* Plugin ID/Package Name: `cordova-plugin-google-analytics`
* Tested Version: 1.8.6

{% hint style="info" %}
サードパーティー製 Cordova プラグインを確認する場合は、カスタムビルドデバッガー ( [Android 版](/products_guide/debugger/installation/debugger_android.md#kasutamubirudo-monaca-debaggnobirudotoinsutru) または [iOS 版](/products_guide/debugger/installation/debugger_ios.md#kasutamubirudo-monaca-debaggnobirudo) ) を作成する必要があります。
{% endhint %}

## デモ

[<img src="https://docs.monaca.io/images/common/import_img.png" alt="" data-size="line">プロジェクトをインポート](https://monaca.mobi/directimport?pid=5ac33902e7888548428b4567)

![](/files/-MgxwK0WDsaUMK_sopqs)

## Monaca クラウド IDE でプラグインを有効にする

1. IDE メニューか&#x3089;**`設定 → Cordova プラグインの管理`**&#x3078;移動します。
2. `Cordova プラグインのインポート`ボタンをクリックします。 次に、\[ZIP ファイル] または \[パッケージ名 / URL] を使用してプラグインをインポートします。

## 使い方

プラグインをプロジェクトへインポート後、トラッキングID の初期化を開始することができます。\[deviceready] イベント後にプラグイン API を使用します。

```javascript
//Replace your app tracking id here
var trackingID="YOUR_APP_TRACKING_ID";

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady(){
    console.log('Google analytics is ready now');
    window.ga.startTrackerWithId(trackingID);
}
```

## API リファレンス

ここでは、[デモ](https://monaca.mobi/directimport?pid=5ac33902e7888548428b4567) で使用されている主な API について説明します。 完全なAPIリファレンスについては、[こちら](https://github.com/danwilson/google-analytics-plugin) を参照してください。

### startTrackerWithId()

Analytics トラッカーを設定します。

```javascript
window.ga.startTrackerWithId(trackingId, [interval]);
```

**パラメーター**

| パラメーター名      | 型      | 説明                                     |
| ------------ | ------ | -------------------------------------- |
| `trackingId` | String | Google Analytics モバイルアプリのプロパティ         |
| `interval`   | Number | \[オプション] 秒単位でのディスパッチ期間 (default: `30`) |

**戻り値**

* `Promise`

**例**

```javascript
window.ga.startTrackerWithId('UA-XXXX-YY', 30);
```

### trackView()

画面を追跡します。

```javascript
window.ga.trackView(title, campaignUrl, [newSession]);
```

**パラメーター**

| パラメーター名       | 型       | 説明                                      |
| ------------- | ------- | --------------------------------------- |
| `title`       | String  | 画面のタイトル                                 |
| `campaignUrl` | String  | 参照を測定するためのキャンペーンURL                     |
| `newSession`  | Boolean | \[オプション] 新しいセッションを作成するには `true` を設定します。 |

**戻り値**

* `Promise`

**例**

```javascript
//To track a Screen (PageView):
window.ga.trackView('Screen Title')

//To track a Screen (PageView) w/ campaign details:
window.ga.trackView('Screen Title', 'my-scheme://content/1111?utm_source=google&utm_campaign=my-campaign')

//To track a Screen (PageView) and create a new session:
window.ga.trackView('Screen Title', '', true)
```

### trackEvent()

イベントを追跡します。

```javascript
window.ga.trackEvent(category, action, [label], [value], [newSession])
```

**パラメーター**

| パラメーター名      | 型       | 説明                                      |
| ------------ | ------- | --------------------------------------- |
| `category`   | String  | イベントカテゴリー (例：'Video')                   |
| `action`     | String  | Action type (例： 'play')                 |
| `label`      | String  | \[オプション] イベントラベル (例：'Fall Campaign')    |
| `value`      | Number  | \[オプション] イベントに関連付けられた数値 (例：42)          |
| `newSession` | Boolean | \[オプション] 新しいセッションを作成するには `true` を設定します。 |

**戻り値**

* `Promise`

**例**

```javascript
//To track an Event
window.ga.trackEvent('Videos', 'play', 'Fall Campaign', 42)

//To track an Event and create a new session:
window.ga.trackEvent('Videos', 'play', 'Fall Campaign', 42, true)
```

### setUserId()

UserId を設定します.

```javascript
window.ga.setUserId(id);
```

**パラメーター**

| パラメーター名 | 型      | 説明                                       |
| ------- | ------ | ---------------------------------------- |
| `id`    | String | 特定のユーザーに関連付けられた一意の識別子は、ヒットごとに送信する必要があります |

**戻り値**

* `Promise`

**例**

```javascript
//user ID for testing purpose
var myUserId="35009a79-1a05-49d7-b876-2b884d0f825b";
window.ga.setUserId(myUserId);
```

### setAppVersion()

特定のアプリのバージョンを設定します。

```javascript
window.ga.setAppVersion(appVersion)
```

**パラメーター**

| パラメーター名      | 型      | 説明       |
| ------------ | ------ | -------- |
| `appVersion` | String | アプリバージョン |

**戻り値**

* `Promise`

**例**

```javascript
window.ga.setAppVersion('1.33.7');
```

### debugMode()

冗長ロギングを有効にします。

```javascript
window.ga.debugMode()
```

**戻り値**

* `Promise`

**例**

```javascript
window.ga.debugMode();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ja.docs.monaca.io/tutorials/google_analytics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
