To integrate Mixpanel with Webflow and add custom code to track specific actions such as video plays, you can use custom attributes in Webflow. Follow these steps for a seamless integration.
1. Set Up Mixpanel
- Create an Account: If you haven’t already, sign up for a Mixpanel account.
- Access Your Token: Navigate to the project settings in Mixpanel to retrieve your project token.
2. Add Mixpanel Script to Webflow
- Go to Project Settings: Log into Webflow and select your project.
- Access Custom Code: Click on the Custom Code tab in the project settings.
- Insert Mixpanel Script: Paste the Mixpanel JavaScript snippet provided by Mixpanel in the Head Code section of your project. Ensure your Mixpanel project token is correctly placed in the snippet.
- Save Changes: Once you have inserted the script, save your changes.
3. Use Custom Attributes for Tracking Events
- Select Element: In your Webflow designer, select the element you want to track (e.g., a video).
- Add Custom Attribute: Go to the settings panel and scroll down to the Custom Attributes section.
- Set Attributes: Add a custom attribute with the name
data-mixpanel-event and value corresponding to the event you want to track (e.g., video-play). - Save and Publish: Once you have added the custom attribute, publish your changes.
4. Add Custom Code for Event Tracking
- Open Custom Code: Go back to Webflow's Custom Code section.
- Insert Tracking Code: Add a script after the Mixpanel initialization script in the Footer Code section. Use JavaScript to listen for the specific event (e.g., video play) and trigger a Mixpanel event using
mixpanel.track(). - Example Event Listener:
- Use document query selectors to target elements with your custom attribute.
- Attach event listeners to these elements to track user interactions.
- Sample Code:
```javascript
document.querySelectorAll('[data-mixpanel-event="video-play"]').forEach(video => {
video.addEventListener('play', () => {
mixpanel.track('Video Play');
});
});
```
- Save and Publish: After adding this code, save and publish your changes again.
Summary
By following these steps, you can effectively integrate Mixpanel with Webflow and use custom attributes to track specific events like video plays. Remember to test your integrations to ensure that the events are being captured correctly in Mixpanel.