Skip to main content

Get Data Into Klaviyo

Course overview
Lesson
2 min read

Walkthrough: Track browsing and capture consent client-side

This lesson is optional. It walks you through the client-side calls, step by step, using Flexie's reformer class example. If you'd rather skip the walkthrough, you can move on to the next lesson.

Scenario: A Flexie member keeps looking at the reformer class pack, then signs up for the newsletter.

Open each step in order. Each one builds on the one before it.

Step 1: Load the snippet only after consent is possible to honor

Don't hardcode the Klaviyo onsite snippet as an executable script tag. Load it as an inert placeholder (type="text/plain") that your consent tool rewrites to text/javascript once the visitor accepts the marketing/analytics category. This keeps "Active on Site" and auto-tracked pageviews from firing pre-consent.

This step happens on your own website, with your own consent tool, so you can't practice it in a test account.

Step 2: Track the product view (Event API, via the Klaviyo object)
javascript
klaviyo.track('Viewed Product', {
  ProductName: 'Reformer Class Pack',
  ProductID: '9237500952725',
  SKU: '10913911010106',
  Categories: ['Classes'],
  URL: window.location.href
});

If you were calling the raw endpoint instead of the Klaviyo object (e.g., from a non-browser client), the equivalent request is:

text
POST https://a.klaviyo.com/client/events/?company_id=PUBLIC_API_KEY
Content-Type: application/json
revision: <current stable revision>
json
{
  "data": {
    "type": "event",
    "attributes": {
      "properties": {
        "ProductName": "Reformer Class Pack",
        "ProductID": "9237500952725",
        "SKU": "10913911010106",
        "Categories": ["Classes"]
      },
      "metric": {
        "data": { "type": "metric", "attributes": { "name": "Viewed Product" } }
      },
      "profile": {
        "data": { "type": "profile", "attributes": {} }
      }
    }
  }
}

Note there's no Authorization header here, the public key goes in the company_id query parameter. An empty profile.data.attributes object is valid for an anonymous visitor; the Klaviyo object handles attaching the visitor's cookie-based identity for you automatically, which is one concrete reason to prefer it over the raw call.

Step 3: Identify the visitor, then capture consent when the newsletter form is submitted

klaviyo.identify() connects the visitor's earlier browsing to their email. It doesn't subscribe them.

javascript
klaviyo.identify({ email: form.email.value });

To capture consent, call Create Client Subscription:

text
POST https://a.klaviyo.com/client/subscriptions/?company_id=PUBLIC_API_KEY
Content-Type: application/json
revision: <current stable revision>
json
{
  "data": {
    "type": "subscription",
    "attributes": {
      "profile": {
        "data": {
          "type": "profile",
          "attributes": {
            "email": "visitor@realmail.com",
            "subscriptions": {
              "email": { "marketing": { "consent": "SUBSCRIBED" } }
            }
          }
        }
      }
    }
  }
}

Only the email channel is included in subscriptions, so this call subscribes the visitor to email marketing without touching the text consent state, even if a phone number happens to be present elsewhere on the profile.

Step 4: Verify

Since client-side calls also return 202 without confirming the identifier was stored correctly, open the profile in the account and confirm both the Viewed Product event and the email subscription actually show up.

Walkthrough: Track browsing and capture consent client-side