やりたいこと
ブレッドボード上にタクトスイッチ(3色)を設置し、押したスイッチごとにGoogleアナリティクスのイベントトラッキングを実行したい。
(スイッチの色(青・黄・緑)ごとにイベントラベルには異なる値を指定する)
例)青色のスイッチを押した場合
Googleアナリティクスに対して、
ヒットタイプ:event
イベントカテゴリ:ArduinoTactSwitch
イベントアクション:pushed
イベントラベル:BLUE
を送信する。
黄色スイッチの場合のイベントラベルは「YELLOW」
緑色スイッチの場合のイベントラベルは「GREEN」
やり方
配線は「ArduinoからPOSTデータを送信してPHPで受け取る方法」の配線図の通りにつなぐ。
プログラムは今回用に作り直した。
Googleアナリティクスの設定
Googleアナリティクスから新しいプロパティを作成する。
トラッキングの対象:モバイルアプリ
トラッキングの方法:Google アナリティクス開発者サービス SDK
プログラム
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
/** * イーサネットシールドと競合するのを防ぐため5,7,9ピンを使う */ #include <SPI.h> #include <Ethernet.h> //MAC byte mac[] = {0x02, 0x02, 0x02, 0x02, 0x02, 0x02}; //local IP byte ip[] = {192, 168, 0, 123}; //http用のURLを使う char HOST[] = "www.google-analytics.com"; String END_POINT = "/collect"; //トラッキングID(各自指定) String TRACKING_ID = "UA-XXXXXXXX-X"; //UUID(バージョン4) https://www.uuidgenerator.net/ とかで適当に生成 String UUID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; EthernetClient client; #define PIN_INPUT_BLUE 5 #define PIN_INPUT_YELLOW 7 #define PIN_INPUT_GREEN 9 void setup() { pinMode(PIN_INPUT_BLUE, INPUT); pinMode(PIN_INPUT_YELLOW, INPUT); pinMode(PIN_INPUT_GREEN, INPUT); Serial.begin(9600); delay(500); while (!Serial) { ; } delay(1000); Ethernet.begin(mac, ip); delay(3000); Serial.print("IP: "); Serial.println(Ethernet.localIP()); Serial.println("Ready..."); Serial.println(); } bool onBlue = false; bool onYellow = false; bool onGreen = false; void loop() { int blue = digitalRead(PIN_INPUT_BLUE); int yellow = digitalRead(PIN_INPUT_YELLOW); int green = digitalRead(PIN_INPUT_GREEN); switch (blue) { case HIGH: if (!onBlue) { Serial.println("BLUE ON"); //青色スイッチが押されたら「BLUE」 post("ArduinoTactSwitch", "pushed", "BLUE", "1"); delay(1000); onBlue = true; } break; case LOW: if (onBlue) { Serial.println("BLUE OFF"); onBlue = false; } break; default: break; } switch (yellow) { case HIGH: if (!onYellow) { Serial.println("YELLOW ON"); //黄色スイッチが押されたら「YELLOW」 post("ArduinoTactSwitch", "pushed", "YELLOW", "1"); delay(1000); onYellow = true; } break; case LOW: if (onYellow) { Serial.println("YELLOW OFF"); onYellow = false; } break; default: break; } switch (green) { case HIGH: if (!onGreen) { Serial.println("GREEN ON"); //緑色スイッチが押されたら「GREEN」 post("ArduinoTactSwitch", "pushed", "GREEN", "1"); delay(1000); onGreen = true; } break; case LOW: if (onGreen) { Serial.println("GREEN OFF"); onGreen = false; } break; default: break; } } bool post(String eventCategory, String eventAction, String eventLabel, String eventValue) { Serial.println("connecting..."); if (client.connect(HOST, 80)) { Serial.println("connected"); String query = "v=1&tid=" + TRACKING_ID + "&cid=" + UUID + "&t=event&ec=" + eventCategory + "&ea=" + eventAction + "&el=" + eventLabel + "&ev=" + eventValue; client.println("POST " + END_POINT + " HTTP/1.1"); client.print("Host: "); client.println(HOST); client.println("User-Agent: Arduino"); client.print("Content-Length: "); client.println(query.length()); client.println("Connection: close"); client.println(); client.print(query); Serial.println(query); Serial.println("sent"); delay(500); client.stop(); return true; } return false; } |
設定したアナリティクスのレポートで「リアルタイム」-「イベント」を選択し、各色のスイッチを押してイベントが取得できていれば成功。