Adding changeId to metrics

ChangeId will enable to aggregate events on change, so we understand
user flow and we can investigate past issues on change

Change-Id: Ib31620a84f8c03a1464386ddf2048312ba97ab79
diff --git a/polygerrit-ui/app/elements/core/gr-router/gr-router.ts b/polygerrit-ui/app/elements/core/gr-router/gr-router.ts
index c2f5678..e0b40b1 100644
--- a/polygerrit-ui/app/elements/core/gr-router/gr-router.ts
+++ b/polygerrit-ui/app/elements/core/gr-router/gr-router.ts
@@ -1540,9 +1540,10 @@
 
   _handleChangeRoute(ctx: PageContextWithQueryMap) {
     // Parameter order is based on the regex group number matched.
+    const changeNum = Number(ctx.params[1]) as NumericChangeId;
     const params: GenerateUrlChangeViewParameters = {
       project: ctx.params[0] as RepoName,
-      changeNum: Number(ctx.params[1]) as NumericChangeId,
+      changeNum,
       basePatchNum: convertToPatchSetNum(ctx.params[4]),
       patchNum: convertToPatchSetNum(ctx.params[6]),
       view: GerritView.CHANGE,
@@ -1550,26 +1551,30 @@
     };
 
     this.reporting.setRepoName(params.project);
+    this.reporting.setChangeId(changeNum);
     this._redirectOrNavigate(params);
   }
 
   _handleCommentRoute(ctx: PageContextWithQueryMap) {
+    const changeNum = Number(ctx.params[1]) as NumericChangeId;
     const params: GenerateUrlDiffViewParameters = {
       project: ctx.params[0] as RepoName,
-      changeNum: Number(ctx.params[1]) as NumericChangeId,
+      changeNum,
       commentId: ctx.params[2] as UrlEncodedCommentId,
       view: GerritView.DIFF,
       commentLink: true,
     };
     this.reporting.setRepoName(params.project);
+    this.reporting.setChangeId(changeNum);
     this._redirectOrNavigate(params);
   }
 
   _handleDiffRoute(ctx: PageContextWithQueryMap) {
+    const changeNum = Number(ctx.params[1]) as NumericChangeId;
     // Parameter order is based on the regex group number matched.
     const params: GenerateUrlDiffViewParameters = {
       project: ctx.params[0] as RepoName,
-      changeNum: Number(ctx.params[1]) as NumericChangeId,
+      changeNum,
       basePatchNum: convertToPatchSetNum(ctx.params[4]),
       patchNum: convertToPatchSetNum(ctx.params[6]),
       path: ctx.params[8],
@@ -1581,6 +1586,7 @@
       params.lineNum = address.lineNum;
     }
     this.reporting.setRepoName(params.project);
+    this.reporting.setChangeId(changeNum);
     this._redirectOrNavigate(params);
   }
 
@@ -1623,9 +1629,10 @@
   _handleDiffEditRoute(ctx: PageContextWithQueryMap) {
     // Parameter order is based on the regex group number matched.
     const project = ctx.params[0] as RepoName;
+    const changeNum = Number(ctx.params[1]) as NumericChangeId;
     this._redirectOrNavigate({
       project,
-      changeNum: Number(ctx.params[1]) as NumericChangeId,
+      changeNum,
       // for edit view params, patchNum cannot be undefined
       patchNum: convertToPatchSetNum(ctx.params[2])!,
       path: ctx.params[3],
@@ -1633,19 +1640,22 @@
       view: GerritView.EDIT,
     });
     this.reporting.setRepoName(project);
+    this.reporting.setChangeId(changeNum);
   }
 
   _handleChangeEditRoute(ctx: PageContextWithQueryMap) {
     // Parameter order is based on the regex group number matched.
     const project = ctx.params[0] as RepoName;
+    const changeNum = Number(ctx.params[1]) as NumericChangeId;
     this._redirectOrNavigate({
       project,
-      changeNum: Number(ctx.params[1]) as NumericChangeId,
+      changeNum,
       patchNum: convertToPatchSetNum(ctx.params[3]),
       view: GerritView.CHANGE,
       edit: true,
     });
     this.reporting.setRepoName(project);
+    this.reporting.setChangeId(changeNum);
   }
 
   /**
diff --git a/polygerrit-ui/app/services/gr-reporting/gr-reporting.ts b/polygerrit-ui/app/services/gr-reporting/gr-reporting.ts
index 08952f6..7035f26 100644
--- a/polygerrit-ui/app/services/gr-reporting/gr-reporting.ts
+++ b/polygerrit-ui/app/services/gr-reporting/gr-reporting.ts
@@ -15,6 +15,8 @@
  * limitations under the License.
  */
 
+import {NumericChangeId} from '../../types/common';
+
 export type EventValue = string | number | {error?: Error};
 
 // TODO(dmfilippov): TS-fix-any use more specific type instead if possible
@@ -106,4 +108,5 @@
   recordDraftInteraction(): void;
   reportErrorDialog(message: string): void;
   setRepoName(repoName: string): void;
+  setChangeId(changeId: NumericChangeId): void;
 }
diff --git a/polygerrit-ui/app/services/gr-reporting/gr-reporting_impl.ts b/polygerrit-ui/app/services/gr-reporting/gr-reporting_impl.ts
index 1b0bf45..657a3f4 100644
--- a/polygerrit-ui/app/services/gr-reporting/gr-reporting_impl.ts
+++ b/polygerrit-ui/app/services/gr-reporting/gr-reporting_impl.ts
@@ -23,6 +23,7 @@
   Timer,
 } from './gr-reporting';
 import {hasOwnProperty} from '../../utils/common-util';
+import {NumericChangeId} from '../../types/common';
 
 // Latency reporting constants.
 
@@ -270,6 +271,7 @@
   eventStart: number;
   eventDetails?: string;
   repoName?: string;
+  changeId?: string;
   inBackgroundTab?: boolean;
   enabledExperiments?: string;
 }
@@ -296,6 +298,8 @@
 
   private _reportRepoName: string | undefined;
 
+  private _reportChangeId: NumericChangeId | undefined;
+
   private _timers: {timeBetweenDraftActions: Timer | null} = {
     timeBetweenDraftActions: null,
   };
@@ -420,6 +424,9 @@
     if (this._reportRepoName) {
       eventInfo.repoName = this._reportRepoName;
     }
+    if (this._reportChangeId) {
+      eventInfo.changeId = `${this._reportChangeId}`;
+    }
 
     const isInBackgroundTab = document.visibilityState === 'hidden';
     if (isInBackgroundTab !== undefined) {
@@ -499,6 +506,7 @@
     this.time(TIMER.DIFF_VIEW_LOAD_FULL);
     this.time(TIMER.FILE_LIST_DISPLAYED);
     this._reportRepoName = undefined;
+    this._reportChangeId = undefined;
     // reset slow rpc list since here start page loads which report these rpcs
     this._slowRpcList = [];
     this.hiddenDurationTimer.reset();
@@ -846,6 +854,10 @@
   setRepoName(repoName: string) {
     this._reportRepoName = repoName;
   }
+
+  setChangeId(changeId: NumericChangeId) {
+    this._reportChangeId = changeId;
+  }
 }
 
 export const DEFAULT_STARTUP_TIMERS = {...STARTUP_TIMERS};
diff --git a/polygerrit-ui/app/services/gr-reporting/gr-reporting_mock.ts b/polygerrit-ui/app/services/gr-reporting/gr-reporting_mock.ts
index 1e75d88..a6472d1 100644
--- a/polygerrit-ui/app/services/gr-reporting/gr-reporting_mock.ts
+++ b/polygerrit-ui/app/services/gr-reporting/gr-reporting_mock.ts
@@ -57,6 +57,7 @@
   reportLifeCycle: () => {},
   reportRpcTiming: () => {},
   setRepoName: () => {},
+  setChangeId: () => {},
   time: () => {},
   timeEnd: () => {},
   timeEndWithAverage: () => {},