Commit eec20c95 authored by Simon Knox's avatar Simon Knox

Merge branch '335067-add-ga-id-to-gitlab-standard-schema' into 'master'

Add Google Analytics ID to gitlab_standard schema

See merge request gitlab-org/gitlab!71242
parents bf888421 a9522b5e
......@@ -25,3 +25,5 @@ export const LOAD_ACTION_ATTR_SELECTOR = '[data-track-action="render"]';
export const URLS_CACHE_STORAGE_KEY = 'gl-snowplow-pseudonymized-urls';
export const REFERRER_TTL = 24 * 60 * 60 * 1000;
export const GOOGLE_ANALYTICS_ID_COOKIE_NAME = '_ga';
import { SNOWPLOW_JS_SOURCE } from './constants';
import { getCookie } from '~/lib/utils/common_utils';
import { SNOWPLOW_JS_SOURCE, GOOGLE_ANALYTICS_ID_COOKIE_NAME } from './constants';
export default function getStandardContext({ extra = {} } = {}) {
const { schema, data = {} } = { ...window.gl?.snowplowStandardContext };
......@@ -8,6 +9,7 @@ export default function getStandardContext({ extra = {} } = {}) {
data: {
...data,
source: SNOWPLOW_JS_SOURCE,
google_analytics_id: getCookie(GOOGLE_ANALYTICS_ID_COOKIE_NAME) ?? '',
extra: extra || data.extra,
},
};
......
......@@ -21,6 +21,7 @@ The [`StandardContext`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/g
| `environment` | **{check-circle}** | string (max 32 chars) | Name of the source environment, such as `production` or `staging` |
| `source` | **{check-circle}** | string (max 32 chars) | Name of the source application, such as `gitlab-rails` or `gitlab-javascript` |
| `plan` | **{dotted-circle}** | string (max 32 chars) | Name of the plan for the namespace, such as `free`, `premium`, or `ultimate`. Automatically picked from the `namespace`. |
| `google_analytics_id` | **{dotted-circle}** | string (max 32 chars) | Google Analytics ID, present when set from our marketing sites. |
| `extra` | **{dotted-circle}** | JSON | Any additional data associated with the event, in the form of key-value pairs |
## Default Schema
......
import { SNOWPLOW_JS_SOURCE } from '~/tracking/constants';
import { SNOWPLOW_JS_SOURCE, GOOGLE_ANALYTICS_ID_COOKIE_NAME } from '~/tracking/constants';
import getStandardContext from '~/tracking/get_standard_context';
import { setCookie, removeCookie } from '~/lib/utils/common_utils';
const TEST_GA_ID = 'GA1.2.345678901.234567891';
const TEST_BASE_DATA = {
source: SNOWPLOW_JS_SOURCE,
google_analytics_id: '',
extra: {},
};
describe('~/tracking/get_standard_context', () => {
beforeEach(() => {
......@@ -10,10 +18,7 @@ describe('~/tracking/get_standard_context', () => {
it('returns default object if called without server context', () => {
expect(getStandardContext()).toStrictEqual({
schema: undefined,
data: {
source: SNOWPLOW_JS_SOURCE,
extra: {},
},
data: TEST_BASE_DATA,
});
});
......@@ -28,9 +33,8 @@ describe('~/tracking/get_standard_context', () => {
expect(getStandardContext()).toStrictEqual({
schema: 'iglu:com.gitlab/gitlab_standard',
data: {
...TEST_BASE_DATA,
environment: 'testing',
source: SNOWPLOW_JS_SOURCE,
extra: {},
},
});
});
......@@ -50,4 +54,15 @@ describe('~/tracking/get_standard_context', () => {
expect(getStandardContext({ extra }).data.extra).toBe(extra);
});
describe('with Google Analytics cookie present', () => {
afterEach(() => {
removeCookie(GOOGLE_ANALYTICS_ID_COOKIE_NAME);
});
it('appends Google Analytics ID', () => {
setCookie(GOOGLE_ANALYTICS_ID_COOKIE_NAME, TEST_GA_ID);
expect(getStandardContext().data.google_analytics_id).toBe(TEST_GA_ID);
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment