# TryNow SDK Methods
## Overview
The TryNow SDK provides integration with Shopify stores to enable try-before-you-buy functionality. It's accessible via the global `window.trynow` object and offers methods for cart manipulation, product handling, and component management.
## Using TryNow SDK Methods
In order to use methods from the TryNow SDK, you must have TryNow initialized in your theme. There are two ways to initialize TryNow.
### Installation
The TryNow SDK is automatically initialized when the app embed is enabled in your Shopify theme. You can access it via the global `window.trynow` object.
### Initialization
You can also make the TryNow SDK available to you by using the initialization method.
```javascript
// Basic initialization
window.trynow.initialize();
// Initialization with configuration
window.trynow.initialize({
myshopifyDomain: 'your-store.myshopify.com',
debug: true,
atcFailsafe: true
});
// Alternative static initialization
TryNow.initialize({
myshopifyDomain: 'your-store.myshopify.com'
});
```
## Configuration Options
The SDK accepts the following configuration options:
| Property | Type | Description | Default |
| ----------------------- | ------- | ----------------------------------------------------- | ---------------------- |
| `myshopifyDomain` | string | The Shopify domain of the store | `window.Shopify?.shop` |
| `storefrontAccessToken` | string | The Storefront API access token (for headless mode) | `undefined` |
| `cartId` | string | The cart ID if one exists already (for headless mode) | `undefined` |
| `debug` | boolean | Enable debug mode for additional console logging | `false` |
| `waitForEvent` | string | Custom event to wait for before initializing | `undefined` |
| `atcFailsafe` | boolean | Enable Add to Cart Failsafe Monitor | `false` |
## Core Properties
| Property | Type | Description |
| ------------- | ------------------- | ------------------------------------------- |
| `cart` | `CartMethods` | Methods for cart manipulation |
| `product` | `ProductMethods` | Methods for product handling |
| `components` | `ComponentRegister` | Access to component registration |
| `headless` | boolean | Whether the SDK is running in headless mode |
| `initialized` | boolean | Whether the SDK has been initialized |
| `config` | `TryNowInitConfig` | Current configuration |
| `cartId` | string | Current cart ID |
| `logger` | `Logger` | Logger instance |
## Methods
### Configuration Methods
#### `initialize(config?: TryNowInitConfig): TryNow`
Initializes the TryNow SDK with the provided configuration.
```javascript
window.trynow.initialize({
myshopifyDomain: 'your-store.myshopify.com',
debug: true
});
```
#### `getConfig(): TryNowInitConfig`
Gets the current configuration of the TryNow SDK.
```javascript
const config = window.trynow.getConfig();
```
#### `getMyshopifyDomain(): string`
Gets the myshopify domain of the store.
```javascript
const domain = window.trynow.getMyshopifyDomain();
```
#### `getVersion(): string`
Gets the SDK version.
```javascript
const version = window.trynow.getVersion();
```
### TryNow Functionality
#### `getTrialPeriodDays(): Promise`
Gets the number of trial period days.
```javascript
window.trynow.getTrialPeriodDays().then(days => {
console.log(`Trial period: ${days} days`);
});
```
#### `getSellingPlanId(): Promise`
Gets the selling plan ID number as a string.
```javascript
window.trynow.getSellingPlanId().then(id => {
console.log(`Selling plan ID: ${id}`);
});
```
#### `hasPassedTryLink(): Promise`
Checks if the gated\_link TryLink has been passed.
```javascript
window.trynow.hasPassedTryLink().then(passed => {
if (passed) {
console.log('User has passed the TryLink gate');
}
});
```
#### `addToCartClicked(): Promise`
Utility method to log TryNow CTA click analytics event. This is automatically called by TryNow CTA buttons.
```javascript
// Only use this if manually creating a CTA button
customButton.addEventListener('click', () => {
window.trynow.addToCartClicked();
});
```
#### `reset(): void`
Clears and reloads product page data, refreshing all registered components.
```javascript
window.trynow.reset();
```
### Component Management
#### `getComponent(id: string): HTMLElement | null`
Gets a registered component by ID.
```javascript
const component = window.trynow.getComponent('my-component-id');
```
#### `defineComponents(): void`
Defines all TryNow custom web components.
```javascript
window.trynow.defineComponents();
```
#### `createVisibilityRuleset(rules: VisibilityRule[]): VisibilityRuleset`
Creates a ruleset for controlling component visibility.
```javascript
// Define the visibility rulesets
const ctaButtonRuleset = window.trynow.createVisibilityRuleset({
// example visibility rule:
rank: 10,
rules: [
{
rank: 0,
ruleTest: () => window.abTest === 'b',
ruleOutcome: () => {
const ctaButton = document.querySelector('#ctaButton');
if (ctaButton) {
ctaButton.style.display = 'block';
}
},
},
],
});
// Apply the rulesets to the components
window.trynow.getComponent('ctaButton', true).then(ctaButton => {
ctaButton.addRuleset(ctaButtonRuleset);
});
```
## Utility Functions
### `waitForShopifyToLoad(): Promise`
Waits for Shopify to load before executing code.
```javascript
window.waitForShopifyToLoad().then(() => {
// Code that depends on Shopify being loaded
});
```
## Headless Mode
When initialized with a `storefrontAccessToken`, the SDK operates in headless mode, which disables certain features that depend on the Shopify theme environment.
```javascript
window.trynow.initialize({
myshopifyDomain: 'your-store.myshopify.com',
storefrontAccessToken: 'your-storefront-access-token',
cartId: 'existing-cart-id' // Optional
});
```
## Events
The SDK dispatches a `trynow:initialized` event when initialization is complete.
```javascript
window.addEventListener('trynow:initialized', () => {
console.log('TryNow SDK has been initialized');
});
```
## Error Handling
Most asynchronous methods return promises that should be handled with try/catch or .catch():
```javascript
window.trynow.getTrialPeriodDays()
.then(days => {
console.log(`Trial period: ${days} days`);
})
.catch(error => {
console.error('Failed to get trial period days:', error);
});
```