Skip to content

Commit

Permalink
Merge 6a839a6 into 857222b
Browse files Browse the repository at this point in the history
  • Loading branch information
ejaffee01 committed Sep 29, 2023
2 parents 857222b + 6a839a6 commit c8eafb2
Show file tree
Hide file tree
Showing 56 changed files with 1,458 additions and 1,623 deletions.
10 changes: 10 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.github/
coverage/
dist/
docs/
etc/
lib/
node_modules/
package*.json
LICENSE
THIRD-PARTY-NOTICES
18 changes: 8 additions & 10 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module.exports = {
extends: [
'plugin:@typescript-eslint/recommended'
],
extends: ['plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
sourceType: 'module'
},
rules: {
indent: ['error', 2, { SwitchCase: 1 }],
Expand All @@ -16,20 +14,20 @@ module.exports = {
'error',
{
named: 'never',
anonymous: 'never',
},
anonymous: 'never'
}
],
'keyword-spacing': ['error'],
'quote-props': ['error', 'as-needed'],
'max-len': [
'error',
{
code: 110,
ignorePattern: '^import\\s.+\\sfrom\\s.+;$',
},
ignorePattern: '^import\\s.+\\sfrom\\s.+;$'
}
],
'@typescript-eslint/semi': ['error'],
'@typescript-eslint/type-annotation-spacing': ['error'],
'@typescript-eslint/type-annotation-spacing': ['error']
},
ignorePatterns: ['lib', 'test-site'],
ignorePatterns: ['lib', 'test-site']
};
27 changes: 27 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Playwright Tests
on: [push, pull_request]

env:
YEXT_API_KEY: ${{ secrets.YEXT_API_KEY }}

jobs:
test-e2e:
timeout-minutes: 5
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/playwright:v1.38.0-jammy
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install dependencies
run: npm ci && npm i --prefix ./test-site
- name: build package
run: npm run build
- name: Run Playwright tests
env:
# Refer to: https://github.com/microsoft/playwright/issues/6500#issuecomment-1577221956
HOME: /root
run: npx playwright test

2 changes: 2 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ on: [push, pull_request]
jobs:
call_run_tests:
uses: yext/slapshot-reusable-workflows/.github/workflows/run_tests.yml@v1
with:
test_script: npx jest
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ lib/
temp/
.env
.vscode/
.idea/
.idea/
/test-results/
/playwright-report/
/playwright/.cache/
**/.DS_Store
5 changes: 5 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run format-fix
git add .
10 changes: 10 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.github/
coverage/
dist/
docs/
etc/
lib/
node_modules/
package*.json
LICENSE
THIRD-PARTY-NOTICES
8 changes: 8 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const config = {
trailingComma: 'none',
tabWidth: 2,
semi: true,
singleQuote: true
};

module.exports = config;
141 changes: 52 additions & 89 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ A Typescript library for sending Yext Analytics events.
[Full Documentation](./docs/analytics.md)

# Yext Analytics
Yext Analytics is a Typescript library for sending analytics events that occur on your digital experiences to the Yext Analytics platform. You can record user actions that we offer out-of-the-box, such as page views and clicks, or custom actions that are unique to your business! Yext uses the same analytics reporting features across Search, Pages, and Chat so these products all use one interface.

- Works in the **browser** only.
Yext Analytics is a Typescript library for sending analytics events that occur on your digital experiences to the Yext Analytics platform. You can record user actions that we offer out-of-the-box, such as page views and clicks, or custom actions that are unique to your business! Yext uses the same analytics reporting features across Search, Pages, and Chat so these products all use one interface.

- Works in the **browser** only.
- 100% **TypeScript**, with detailed analytics event models
- Compatible with both **CommonJS** and **ES6** imports

Expand All @@ -28,103 +29,126 @@ First, install the library via [npm](https://www.npmjs.com/get-npm):
```bash
npm install @yext/analytics
```

### Initialize Analytics Reporter

Next, import and initialize the library in your application. When initializing your analytics reporter, you only need to provide an API Key that has access to the Events API. Other attributes such as your business ID will be automatically inferred. You can acquire this API key in the developer console of the Yext Platform.

```ts
import { Analytics } from "@yext/analytics";
import { analytics } from '@yext/analytics';

// Root analytics service with no defaults.
const rootAnalytics = new Analytics({ key: "MY_API_KEY" });
const rootAnalytics = analytics({ key: 'MY_API_KEY' });
```

In many cases, you might need to repeatedly specify the same properties, such as a Pages site ID or Chat bot ID. Yext Analytics allows you to avoid having to repeatedly specify the same code by allowing you to set **default values**.

You can add a `.with()` method to the root analytics service you initialized, which returns a new analytics object with the specified JSON merged on top of the existing defaults.

```ts
import { Analytics } from "@yext/analytics";
import { Analytics } from '@yext/analytics';

// Root analytics service with no defaults.
const rootAnalytics = new Analytics({ key: "MY_API_KEY" });
const rootAnalytics = new Analytics({ key: 'MY_API_KEY' });

// Pages analytics service with Pages defaults.
const pageAnalytics = rootAnalytics.with({ pages: { siteId: 123 } });
const pageAnalytics = rootAnalytics.with({ pages: { siteId: 123 } });

// Chat analytics service with both Chat **and** Pages defaults.
const chatAnalytics = pageAnalytics.with({ chat: { botId: "my-bot" } });
const chatAnalytics = pageAnalytics.with({ chat: { botId: 'my-bot' } });
```

Calling `pageAnalytics.report()` sends an event with the `pages` object, plus anything passed to `report`. Calling `chatAnalytics.report()` sends an event with both the `pages` and `chat` objects, plus anything passed to `report`. You can override the default values defined in the .with() method by sending them along with the event.

For other configuration features, see [AnalyticsConfig.ts](/docs/analytics.analyticsconfig.md)

### Fire an Event
Now that we’ve initialized our analytics reporter, we can fire an event! This sends a `CHAT_IMPRESSION` event type, along with a `sessionId`, a `pages.siteId`, and a `chat.botId`.

Now that we’ve initialized our analytics reporter, we can fire an event! This sends a `CHAT_IMPRESSION` event type, along with a `sessionId`, a `pages.siteId`, and a `chat.botId`.

```ts
chatAnalytics.report({
action: "CHAT_IMPRESSION"
action: 'CHAT_IMPRESSION'
});
```
### Additional Configuration

### Additional Configuration

#### Session Tracking

Session tracking is now available for Chat, Pages, and Search. Yext uses a browser-based method (sessionStorage) to track this. By default, session tracking is **enabled** in **both** the US and EU environments. This can be disabled by setting `sessionTrackingEnabled` to `false`.

When `sessionTrackingEnabled` is set to `true`, Analytics will automatically generate a ULID for `sessionId` and bind that ULID to events from the same browser session. Users may also provide their own `sessionId`, which takes precedence over the auto-generated ID by Analytics. [To read more about how we generate ULIDs, check out ulidx.](https://github.com/perry-mitchell/ulidx)

#### Custom Events

You can also send custom analytics events.

```ts
pagesAnalytics.report({
action: "C_MY_CUSTOM_EVENT"
action: 'C_MY_CUSTOM_EVENT'
});
```
Additionally, you can send arbitrary conversion events by specifying a `value` JSON object with a dollar `amount` and a `currency` in ISO format.

Additionally, you can send arbitrary conversion events by specifying a `value` JSON object with a dollar `amount` and a `currency` in ISO format.

```ts
chatAnalytics.report({
action: "C_CONVERSION_EVENT"
action: 'C_CONVERSION_EVENT',
value: {
amount: 10,
currency: "USD",
},
currency: 'USD'
}
});
```

To learn more about sending conversion events, see our [API documentation](https://hitchhikers.yext.com/docs/eventsapis/events/events#operation/sendEvents).

#### Custom Properties

You can attach custom properties to your analytics events by specifying either `customTags` or `customValues` with your request. `customTags` represent up to ten **string** key-value pairs and `customValues` represent up to ten **numeric** key-value pairs.

For example, if I set up an `ORDER` event for my restaurant and wanted to track whether a promotional code was used on the order, I could add an `promoCode` custom tag to the event.

```ts
pagesAnalytics.report({
action: "C_CONVERSION_EVENT",
sessionId: "e790f75d-4f1e-4a1b-b57b-9a456019b176",
action: 'C_CONVERSION_EVENT',
sessionId: 'e790f75d-4f1e-4a1b-b57b-9a456019b176',
value: {
amount: 35.50,
currency: "USD",
amount: 35.5,
currency: 'USD'
},
customTags: {
"promoCode": "SPRING15OFF"
promoCode: 'SPRING15OFF'
}
});
```

Additionally, if I wanted to record the discount amount of the promotion, I could add a `promoDiscount` custom value to the `ORDER` event.

```ts
pagesAnalytics.report({
action: "C_CONVERSION_EVENT",
sessionId: "e790f75d-4f1e-4a1b-b57b-9a456019b176",
action: 'C_CONVERSION_EVENT',
sessionId: 'e790f75d-4f1e-4a1b-b57b-9a456019b176',
value: {
amount: 35.50,
currency: "USD",
amount: 35.5,
currency: 'USD'
},
customTags: {
"promoCode": "SPRING15OFF"
promoCode: 'SPRING15OFF'
},
customValues: {
"promoDiscount": 41.76
promoDiscount: 41.76
}
});
```

### Debugging

We use `fetch()` + `keepalive` by default in [supported browsers](https://developer.mozilla.org/en-US/docs/Web/API/fetch) to make debugging easier. For browsers like Firefox that do not support `keepalive`, [we use the Beacon API](https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API). Users can set `forceFetch: true` in their config, which will make these browsers use `fetch()` instead of the `Beacon API`. Be warned, since `forceFetch` uses `fetch()` without `keepalive`, **requests in progress for browsers like FireFox will be canceled if the page is unloaded**.

## Module support

- The ESM (ES6) build will be used automatically by module bundlers that support it (e.g. Webpack). It can be specified directly by importing `@yext/analytics/lib/esm`
- The CommonJS build will be used automatically by Node, but it can be specified directly by importing `@yext/analytics/lib/commonjs`

Expand All @@ -135,64 +159,3 @@ Yext Analytics is an open-sourced library licensed under the [BSD-3 License](./L
## Third Party Licenses

The licenses of our 3rd party dependencies are collected here: [THIRD-PARTY-NOTICES](./THIRD-PARTY-NOTICES).

## Appendix

### Merge Operations
When we merge the JSON, we abide by the following guidelines. For primitives:
* Merging an existing key with null/undefined **deletes the key**.
* Merging an existing key with a non-null value **updates the value**.
* Merging a new key **adds the new value**.

```ts
const x = {
foo: "bar",
num: 123,
baz: "remove me"
}

const y = {
num: 789,
bool: True
baz: null
}

merge(x,y)
/** ---------- Result -------------
* {
* foo: "bar",
* num: 789,
* bool: True
* }
*/
```

For objects, we recursively apply the above rules within the object. Merging multi-level objects strictly combines values on a level-by-level basis, so the first level of each JSON is merged, then the next, until there is nothing left to merge.

```ts
const x = {
foo: {
bar: {
baz: 123
}
}
}

const y = {
foo: {
baz: 789
}
}

merge(x,y)
/** ---------- Result -------------
* {
* foo: {
* bar: {
* baz: 123
* },
* baz: 789
* }
* }
*/
```
Loading

0 comments on commit c8eafb2

Please sign in to comment.