> ## Documentation Index
> Fetch the complete documentation index at: https://docs.guidewhale.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Methods

> A list of methods and properties available in the GuideWhale Javascript SDK.

To install the SDK, please follow the instructions in the [installation](installation) section.

All methods below are called using the loaded `window.gwhale` instance.

## init

<Note>This method needs to be called before calling any other methods below.</Note>

Used to initialize GuideWhale app environment and identify user, company and their membership.

* For single page applications (SPA) this method should only be called when page is initially loaded.
* For multipage application this method should be called on every page load

For a detailed parameter guide see the [installation](installation) section.

<ParamField path="environment_code" type="string" required>
  Code of the app environment to initialize. This can be found in our dashboard under
  <a href="https://app.guidewhale.com?to=/settings/environments" target="_blank">Settings > Environments</a>
  or <a href="https://app.guidewhale.com?to=/settings/installation" target="_blank">Settings > Installation</a>.
</ParamField>

<ParamField path="options" type="object">
  Initialization options, including user, company and membership properties.

  <Expandable title="Options" defaultOpen>
    <ParamField path="signature" type="string">
      User verification signature generated by your backend from user\_id and app environment secret key. See [installation](installation) section for more details.

      **Only required if user.user\_id is set and user has given their consent to data collection.**
    </ParamField>

    <ParamField path="gdpr" type="boolean">
      If we should consider GDPR consent. Enable this if you're dealing with users who have not given their consent for data collection.
    </ParamField>

    <ParamField path="gdpr_consent" type="boolean">
      When GDPR option is enabled, this field should be set to true if the user has given their consent for data collection. If consent has not been given, user.user\_id will be anonymized for each session.
    </ParamField>

    <ParamField path="user" type="object">
      User identification properties.

      <Expandable title="User Properties" defaultOpen>
        <ParamField path="user_id" type="string | null" required>
          Unique user identifier. Set to null if you're dealing with an anonymous user.
        </ParamField>

        <ParamField path="email" type="string">
          User's email address.
        </ParamField>

        <ParamField path="image_url" type="string">
          URL of user's profile image.
        </ParamField>

        <ParamField path="first_name" type="string">
          User's first name.
        </ParamField>

        <ParamField path="last_name" type="string">
          User's last name.
        </ParamField>

        <ParamField path="date_signed_up" type="date">
          When user has signed up for your application.
        </ParamField>

        <ParamField path="..." type="string | number | boolean | date">
          Any other custom user property you want to add.
        </ParamField>
      </Expandable>
    </ParamField>

    <ParamField path="company" type="object">
      Company identification properties.

      <Expandable title="Company Properties" defaultOpen>
        <ParamField path="company_id" type="string" required>
          Unique company identifier.
        </ParamField>

        <ParamField path="name" type="string">
          Company name.
        </ParamField>

        <ParamField path="image_url" type="string">
          URL of company logo image.
        </ParamField>

        <ParamField path="plan" type="string">
          Company's subscription plan.
        </ParamField>

        <ParamField path="date_signed_up" type="date">
          When company has signed up for your application.
        </ParamField>

        <ParamField path="date_renewal" type="date">
          When company's subscription is set to renew.
        </ParamField>

        <ParamField path="mrr" type="number">
          Company's monthly recurring revenue.
        </ParamField>

        <ParamField path="license_count" type="number">
          Number of licenses company has purchased.
        </ParamField>

        <ParamField path="industry" type="string">
          Company industry.
        </ParamField>

        <ParamField path="size" type="number">
          Company employee count.
        </ParamField>

        <ParamField path="..." type="string | number | boolean | date">
          Any other custom company property you want to add.
        </ParamField>
      </Expandable>
    </ParamField>

    <ParamField path="membership" type="object">
      User company membership identification properties.

      <Expandable title="Membership Properties" defaultOpen>
        <ParamField path="is_primary" type="boolean">
          If user is a primary contact of a company.
        </ParamField>

        <ParamField path="role" type="string">
          User's role in a company.
        </ParamField>

        <ParamField path="..." type="string | number | boolean | date">
          Any other custom membership property you want to add.
        </ParamField>
      </Expandable>
    </ParamField>
  </Expandable>
</ParamField>

```javascript Example lines icon={null} theme={null} theme={null}
gwhale.init('prod-1234abcd', {
    gdpr: true,
    gdpr_consent: true,
    signature: '5e7001282790905aa22523e2c0c5e7cd08046b79fd8546b3bb43a5578704f683',
    user: {
        user_id: '123',
        email: 'john.smith@acme.com',
        first_name: 'John',
        last_name: 'Smith',
        date_signed_up: '2022-01-01',
        custom_property: 'value1'
    },
    company: {
        company_id: '123',
        name: 'ACME',
        plan: 'Enterprise',
        date_signed_up: '2022-01-01',
        date_renewal: '2025-01-01',
        mrr: 999,
        license_count: 10,
        industry: 'Construction',
        size: 30,
        custom_property: 'value1'
    },
    membership: {
        is_primary: true,
        role: 'user',
        custom_property: 'value1'
    }
});
```

## track

Used to programmatically track user events that are not taggable or automatically captured by GuideWhale.

<ParamField path="name" type="string" required>
  The name of the event to track.
</ParamField>

<ParamField path="properties" type="object">
  Contextual properties associated with this event.
</ParamField>

```javascript Example lines icon={null} theme={null} theme={null}
gwhale.track('item_purchased', {
    name: 'Pro Subscription',
    price: 1000.0
});
```

## onRedirect

Used to handle redirects without a page reload when GuideWhale content wants to redirect to another page.

<ParamField path="handler" type="(url: string) => void" required>
  Handler function to handle redirects without a page reload when GuideWhale content wants to redirect to another page.
</ParamField>

```javascript Example lines icon={null} theme={null} theme={null}
gwhale.onRedirect((url: string) => {
    navigate(url);
});
```

## reset

Remove currently identified user and company. You will have to call the `init` function again to re-identify the user.

```javascript Example lines icon={null} theme={null} theme={null}
gwhale.reset();
```

[//]: # "## on"

[//]: # "Used to subscribe to events emitted by GuideWhale SDK. List of events can be found in the [integration user events](/integrations/user-events) section."

[//]: #

[//]: # "Function will return the newly added event handler function reference that can be used to unsubscribe from the event."

[//]: #

[//]: # "<ParamField path=\"name\" type=\"string\" required>"

[//]: # "    The name of the event to subscribe to."

[//]: # "</ParamField>"

[//]: #

[//]: # "<ParamField path=\"callback\" type=\"(properties: Record<string, any>) => function\" required>"

[//]: # "    Event callback function."

[//]: # "</ParamField>"

[//]: #

[//]: # "```javascript Example lines icon={null} theme={null}"

[//]: # "const onGuideCompleted = gwhale.on('guide_completed',"

[//]: # "    (properties: Record<string, any>) => {"

[//]: # "        if(properties.title === 'Onboarding Guide'){"

[//]: # "            user.completed_onboarding = true;"

[//]: # "        }"

[//]: # "    }"

[//]: # ");"

[//]: # "```"

[//]: #

[//]: #

[//]: # "## off"

[//]: # "Used to unsubscribe from events emitted by GuideWhale SDK."

[//]: #

[//]: # "<ParamField path=\"name\" type=\"string\" required>"

[//]: # "    The name of the event to unsubscribe to."

[//]: # "</ParamField>"

[//]: #

[//]: # "<ParamField path=\"callback\" type=\"(properties: Record<string, any>) => void\" required>"

[//]: # "    Event callback function."

[//]: # "</ParamField>"

[//]: #

[//]: # "```javascript Example lines icon={null} theme={null}"

[//]: # "gwhale.off('guide_completed', onGuideCompleted);"

[//]: # "```"

## setUserProperties

Helper function to set user properties of currently identified user without having to call the `identify` function again.

<ParamField path="properties" type="object" required>
  New property values.
</ParamField>

```javascript Example: set value lines icon={null} theme={null} theme={null}
gwhale.setUserProperties({
    count: 10
});
```

```javascript Example: add to existing property value lines icon={null} theme={null} theme={null}
// NOTE: Addition is limited to numeric values
gwhale.setUserProperties({
    count: { add: 5 }
});
```

```javascript Example: subtract from existing property value lines icon={null} theme={null} theme={null}
// NOTE: Subtraction is limited to numeric values
gwhale.setUserProperties({
    count: { subtract: 5 }
});
```

## setCompanyProperties

Helper function to set company properties of currently identified user without having to call the `identify` function again.

<ParamField path="properties" type="object" required>
  New property values.
</ParamField>

```javascript Example: set value lines icon={null} theme={null} theme={null}
gwhale.setCompanyProperties({
    plan: 'Enterprise'
});
```

```javascript Example: add to existing property value lines icon={null} theme={null} theme={null}
// NOTE: Addition is limited to numeric values
gwhale.setCompanyProperties({
    count: { add: 5 }
});
```

```javascript Example: subtract from existing property value lines icon={null} theme={null} theme={null}
// NOTE: Subtraction is limited to numeric values
gwhale.setCompanyProperties({
    count: { subtract: 5 }
});
```

## setMembershipProperties

Helper function to set user company membership properties of currently identified user without having to call the `identify` function again.

<ParamField path="properties" type="object" required>
  New property values.
</ParamField>

```javascript Example: set value lines icon={null} theme={null} theme={null}
gwhale.setMembershipProperties({
    position: 'Manager'
});
```

```javascript Example: add to existing property value lines icon={null} theme={null} theme={null}
// NOTE: Addition is limited to numeric values
gwhale.setMembershipProperties({
    count: { add: 5 }
});
```

```javascript Example: subtract from existing property value lines icon={null} theme={null} theme={null}
// NOTE: Subtraction is limited to numeric values
gwhale.setMembershipProperties({
    count: { subtract: 5 }
});
```
