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. That solution utilizes Shopify lifecycle events to pass the parameters and should be used only when you set your Buy button action to “Add product to cart”. The solution discussed below is meant to be used when you set that same action in your Buy button configuration to “Direct to checkout”
FYI:
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 by defining the objective so we are all on the same page.
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.
3 Comments
Chandrakanta
Currently Shopify has updated the CDN version to 3, could you please create another guide on the same how we can tackle in upcoming days?
Shad Malik
Hi Chandrakanta - CDN version update has no effect on this implementation. Whats the trouble you're having?