A while ago, I wrote a solution to pass UTM parameters in Shopify buy Button checkout from your landing page URL to capture traffic sources. This method utilized the Shopify lifecycle events to pass the parameters. This is an updated solution that utilizes Product Component Customization instead.
There are lots of discussions around manipulating Shopify button checkout URL. Some want to use it pass parameters for their local site, others to pass a parameter for a discount code, but perhaps the most sought after problem is how to pass UTM parameters to the the checkout page. It is from one of these discussions on Github about passing a discount code that I took this solution from.
It’s important to note that there is an open feature request on Git for adding line items with custom attributes to the Shopify Buy Button. The implementation of this feature could significantly simplify the customization of the button’s destination, akin to the capabilities available in the JS Buy SDK. I recommend subscribing to updates on this issue to stay informed.
Alright, lets dig into the solution.
But wait! before we start talking about the solution, I just want to put out a disclaimer:
This solution has been tested only when you want users to go straight to the checkout page (without modals and add to cart functionality in the button). For other scenarious, feel free to test the implementation and write in comments what you find 🙂
Lets really get started now by defining our objective 😉
Objective:
Our goal is to modify the Shopify Buy Button so that it not only facilitates the buying process but also retains valuable campaign tracking information through UTM parameters. This ensures that when a user proceeds to the checkout, the UTM parameters are appended to the URL, allowing for accurate tracking of user engagement and campaign effectiveness right through the purchase process.
Process Breakdown:
Lets first review what we’ll do step by step.
UTM Parameter Capture and Serialization
We first define a function that will be responsible to capture the UTM parameters from your landing page (much like in our previous solution) where your Buy Button is hosted. We’ll call this getUTMParameters.
What it does?
URL Parsing: This function uses the browser’s URL parsing capabilities to locate and store each UTM parameter present. It filters parameters to ensure only those that are UTM-related (i.e., those starting with “utm_”) are captured.
Serialization: Once captured, these parameters are serialized into a string that can be appended to URLs, preparing them to be included in the checkout URL.
Shopify Buy Button Initialization
You do not have to worry about this part. This is automatically done via the code you get for the buy button but I just add it here for context.
Script Injection: The script dynamically loads Shopify’s Buy Button SDK from their CDN, ensuring the latest version of the library is used.
Client Initialization: It initializes the Shopify Buy Button client with the store’s domain and an access token, setting up the necessary credentials for interactions with Shopify’s backend.
Store domain: Make sure that your domain URL is correctly added here. In most cases, your buy button contains a line of code with the domain set as your-domain.myshopify.com
. This has 2 implications:
- The checkout URL where users are redirected on buy button click would be
your-domain.myshopify.com/checkouts/
- In Google Analytics, you’ll have to set up a cross-domain tracking and add this subdomain in your property settings.
Product Component Customization
Component Creation: The script creates a product
component using the createComponent
method, which represents a purchasable item on the website.
Custom Button Destination: Traditionally, the buttonDestination
property defines where the button leads (e.g., a cart or direct checkout). Here, it is overridden with a custom function that not only initiates checkout but also modifies the checkout URL.
Checkout Process Manipulation: The custom function intercepts the default checkout process. Upon button click, it:
- Triggers a user event for analytics.
- Creates a checkout session using the Shopify API with the selected product variant and quantity.
- Modifies the resulting checkout URL by appending the serialized UTM parameters, ensuring that marketing data is retained.
- Redirects the user to this new URL in a new popup.
Now that we have the process laid out. Lets look at the code.
0 Comments