Javascript shared methods
npm install cliengo-fe-utilsJs library to share useful methods
``bash`
npm install cliengo-fe-utils
or
`bash`
yarn add cliengo-fe-utils
`javascript
import { method } from 'cliengo-fe-utils';
method();
`
`javascript
import { copyToClipboard } from 'cliengo-fe-utils';
async function myFunction() {
const hasSuccess = copyToClipboard('Hello world');
if (hasSuccess) {
// ...do stuff
}
}
`
*
`javascript
import { isEmpty } from 'cliengo-fe-utils';
async function myFunction() {
const value = isEmpty('Hello world');
//value = false
const value2 = isEmpty('');
//value2 = true
}
`
*$3
Its a Class which uses the singleton pattern to set a unique instance with an initial configuration ready to be used across your application.
This was developed thinking on make the iframe communication with its parent much easier.
methods
setInstance
Initialize the instance with the configuration which will be used in the whole application
params:
| Name | Required | Type | Default | Desciption |
| senderId | Yes | string | N/A | Indicates the name of the child application |
| target | No | string | '*' | Indicates the URI of the parent |
postMessage
Send messages to parent
params:
| Name | Required | Type | Default | Desciption |
| action | Yes | string | N/A | It's the key of the action which will be validated in the parent |
| params | No | JSON | {} | Send additional data to the parent ex. IDs & flags |
#### Usage
`javascript
import { IframeMessagePublisher } from 'cliengo-fe-utils';
// Initialize the instance
IframeMessagePublisher.setInstance('my-project');
// Use where you need
IframeMessagePublisher.postMessage('do-stuff');
IframeMessagePublisher.postMessage('do-stuff-with-params', {
refresh: true,
id: 12345
});
`
*$3
methods
initialize
Initialize the analytics tracking
params: Object{segmentWriteKey, sendAnalytics, account, user}
properties:
| Name | Required | Type | Default | Desciption |
| sendWriteKey | Yes | string | N/A | key for tracking |
| sendAnalytics | No | string | 'YES' | flag to allow/disallow analytics tracking |
| account | Yes | JSON | N/A | account(company) information |
| user | Yes | JSON | N/A | user operator information |
#### Usage
`javascript
import { Analytics } from 'cliengo-fe-utils';
// Initialize the instance
Analytics.initialize({
segmentWriteKey: 'XXXXXXXXX',
sendAnalytics: 'YES',
account: JsonObject,
user: JsonObject
});
`
trackEvent
Wrapper around the Segment's trackEvent method.
it is how you tell Segment about which actions your users are performing on your site.
params: Object{
eventName,
category,
action,
...moreProps
}
properties:
| Name | Required | Type | Default | Desciption |
| eventName | Yes | string | N/A | Name of the event |
| category | No | string | N/A | category of the event |
| action | No | string | N/A | usually matches with eventName |
| label | No | string | N/A | optional value |
rest of the properties depends on the case
#### Usage
`javascript
import { Analytics } from 'cliengo-fe-utils';
Analytics.trackEvent({
eventName: 'user_registration',
category: 'register',
action: 'user_registration'
});
`
trackPage
Wrapper around method Segment's page method.
it lets you record page views on your website, along with optional information about the page being viewed.
params:
| Name | Required | Type | Default | Desciption |
| path | Yes | string | N/A | path of the page |
| websiteId | No | string | " " or undefined | id of selected website |
| url | No | string | window.location.href | url of current page |
rest of the properties depends on the case
#### Usage
`javascript
import { Analytics } from 'cliengo-fe-utils';
Analytics.trackPage('/live/visitors');
`
setTraits
Lets you set the information about the current user and account.
params:
| Name | Required | Type | Default | Desciption |
| account | Yes | JSON | N/A | account(company) information |
| user | Yes | JSON | N/A | user operator information |
#### Usage
`javascript
import { Analytics } from 'cliengo-fe-utils';
Analytics.setTraits(account, user);
`
isInitialized
Indicates if analytics script is already initialized.
#### Usage
`javascript
import { Analytics } from 'cliengo-fe-utils';
Analytics.isInitialized();
`
isAnonymousMode
Indicates if analytics don't count with account and user information.
#### Usage
`javascript
import { Analytics } from 'cliengo-fe-utils';
Analytics.isAnonymousMode();
`
Some flags:
This utils set some flags at window level.
- window.analytics.initialized
- window.analyticsInAnonymousMode
*
1. Check out repo and create a branch
1. Do your thing
1. Bump version following SemVer
1. Transpile code with yarn buildgit tag X.Y.Z`
1. Create a tag with the same name as the new version, otherwise the new version won't be downloadable!
1. Submit a PR
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.