This guide will show you how to use Google Tag Manager (GTM) to effectively track purchases without modifying your Shopify theme code.
Overview
- Objective: Track purchase conversions in Convert Experiences on Shopify using GTM.
- Prerequisites:
- An existing GTM event that fires on the Purchase Confirmation Page.
- Access to your Shopify store's GTM container.
- Convert Experiences installed and running on your Shopify store.
- Key Benefits:
- Simplifies tracking by using GTM.
- Avoids modifying Shopify's theme code.
- Ensures accurate conversion tracking for your experiments.
Step-by-Step Guide
1. Add Code to Your Convert Experiences Global Project JS
To ensure that Convert Experiences can track and attribute conversions correctly, you need to add specific code to your project's Global JavaScript settings in the Convert Experiences app.
Steps:
-
Log in to Convert Experiences:
- Navigate to your Convert Experiences dashboard.
-
Access Project Settings:
- Go to Projects and select the project you're working on.
-
Navigate to Global Project JS:
- In the left-hand menu, click on Configuration.
- Select Global Project JS.
-
Add the Following Code:
-
Explanation:
- This script collects necessary attributes from Convert Experiences and stores them in
localStorage
asconvert_attributes
. - It ensures that the tracking code on the Purchase Confirmation Page can access these attributes to send conversion data.
Save the Changes:
- Click Save to apply the changes to your project.
- This script collects necessary attributes from Convert Experiences and stores them in
Note: If you are using the legacy tracking code (the non-Beta version) of Convert Experiences, please refer to the Convert Experiences documentation for the appropriate code to use in the Global Project JS section.
2. Prepare the Tracking Code for GTM
You will create a GTM Custom HTML Tag that sends purchase conversion data to Convert Experiences when the GTM event fires on the Purchase Confirmation Page.
Code for the GTM Custom HTML Tag:
Explanation:
- This script retrieves
convert_attributes
fromlocalStorage
and sends conversion data to Convert Experiences. - It uses GTM variables to obtain the transaction amount, transaction ID, and product count.
- The code includes error handling and debug logs to assist with testing and troubleshooting.
3. Configure GTM Variables
Ensure the following GTM variables are set up in your GTM container:
-
OrderAmount (
)
- Type: Data Layer Variable
- Data Layer Variable Name: The name of the data layer variable that holds the transaction amount (e.g.,
transactionTotal
).
-
TransactionId (
)
- Type: Data Layer Variable
- Data Layer Variable Name: The name of the data layer variable that holds the transaction ID (e.g.,
transactionId
). - Note: If
transactionId
is not available, you can comment out the related code as indicated in the script.
-
ProductCount (
)
-
Type: Custom JavaScript Variable
-
Code:
javascriptCopy codefunction() {
return .length;
} -
Dependencies: Ensure that
is another GTM variable (usually a Data Layer Variable) that contains an array of the purchased products.
-
-
PurchaseGoalId (
) (Optional)
- Type: Constant Variable
- Value: Your Convert Experiences Purchase Goal ID (e.g.,
'100136097'
). - Note: Alternatively, you can hardcode the
purchase_goalid
directly in the script.
4. Add the Custom HTML Tag to GTM
-
Create a New Tag
- In your GTM container, click on Tags and then New.
- Name the tag (e.g., "Convert Experiences Purchase Tracking").
-
Configure Tag Type
- Choose Custom HTML Tag.
- Paste the tracking code from Step 2 into the HTML field.
-
Set Up Triggering
- Click on Triggering and select the trigger that fires on the Purchase Confirmation Page.
- If you have a specific event or page view that occurs upon purchase completion, use that trigger.
-
Save the Tag
- Review your tag configuration and save it.
5. Adjust the Script Variables
-
Replace Placeholder Variables
-
Replace
,
, and
in the script with the exact names of your GTM variables, including the double curly braces.
-
Example:
javascriptCopy codevar transactionAmount = parseFloat(0);
var transactionId = '0';
var productCount = parseInt(0);
-
-
Set the Purchase Goal ID
- Replace
'XXXXXXXXX'
invar purchase_goalid = 'XXXXXXXXX';
with your actual Convert Experiences Purchase Goal ID. - If using a GTM variable for
purchase_goalid
, ensure it is correctly set up and replace the placeholder accordingly.
- Replace
6. Handle
transactionId
AvailabilityIf
transactionId
is not available in your data layer or GTM variables:-
Comment Out or Remove
transactionId
-Related Code-
Comment out the variable declaration:
javascriptCopy code// var transactionId = ''; // Commented out because transactionId is not available
-
Comment out the line in the
post
object:javascriptCopy code// tid: transactionId,
-
Comment out or remove the code that deletes
'tid'
:javascriptCopy code// if (!transactionId || transactionId === '' || transactionId === '') {
// delete post.tid;
// }
-
-
Adjust Debug Logging
-
Update the debug log in the
postTransaction
function:javascriptCopy codedebugLog("Transaction ID: N/A");
-
7. Test the Implementation
Before publishing your GTM container, thoroughly test the implementation:
-
Enable Debug Mode
- In GTM, click on Preview to enable debug mode on your website.
-
Perform a Test Purchase
- Go through the purchase process on your Shopify store to reach the Purchase Confirmation Page.
-
Verify the Tag Execution
- In the GTM debug panel, ensure that the "Convert Experiences Purchase Tracking" tag fires as expected.
-
Check the Console Logs
- Open the browser console to view debug logs if
DEBUG
is set totrue
. - Look for any errors or warnings that may indicate issues.
- Open the browser console to view debug logs if
-
Monitor Network Requests
- In the browser's developer tools, check the Network tab for the
fetch
requests made to the Convert tracking endpoint. - Verify that the requests are successful (status code 200) and the payload contains the correct data.
- In the browser's developer tools, check the Network tab for the
8. Publish Your Changes
Once you've confirmed that everything works correctly:
-
Submit Your GTM Container for Publishing
- In GTM, click on Submit to publish your changes.
- Provide a descriptive version name and notes for future reference.
-
Monitor Conversion Tracking
- After publishing, monitor your Convert Experiences dashboard to ensure that conversions are being tracked accurately.
Debugging and Support
-
Debug Mode
- Keep
DEBUG
set totrue
during testing to receive detailed logs. - Set it to
false
in the production environment to disable console logs.
- Keep
-
Error Handling
- The script includes error handling to catch issues with
convert_attributes
retrieval and network requests. - Check the console for error messages to assist in troubleshooting.
- The script includes error handling to catch issues with
Customization
-
Transaction Outliers
- The script filters out transactions that are outside the
min_order_value
andmax_order_value
specified inconvert_attributes
. - Adjust these values in your Convert Experiences settings if needed.
- The script filters out transactions that are outside the
Conclusion
By integrating this method, you can effectively track purchase conversions in Convert Experiences on your Shopify store using an existing GTM event. This approach streamlines the tracking process, reduces dependencies on Shopify-specific events, and leverages GTM's flexibility for managing tags and triggers.
For further assistance or questions, refer to the Convert Experiences documentation or reach out to their support team.
-