skip to Main Content

I need access to the URL parameters in Shopify when a customer visits my shop.
For example, the customer calls up my shop with www.myshop.com/id=1, I am interested in the ID.
This ID should then be added to the Shopify database after the purchase has been made.

Later I would like to read out the data in an external project and be able to determine exactly which ID generated how much sales in which period.

Can someone give me a hint how / where to start?

2

Answers


  1. Chosen as BEST ANSWER

    A possible solution would be to read the url parameters from the Order-Webhook under the referring_site-Element. It usually looks like this:

    "referring_site": "https://{shop}.myshopify.com/products/testprodukt-1"

    But the problem is that the parameters disappear when the customer changes the page. So it is not shown on the referring_site-Element of the webhook.

    So my plan looks like this:

    Insert a Shopify-Scripttag:

    'scripttags' => [
        [
            'src' => 'https://{domain}/scripttags/dummy.js',
            'event' => 'onload',
            'display_scope' => 'online_store'
        ]
    ]
    

    dummy.js:

    // some cookie functions
    function setCookie(name,value,days) {
        var expires = "";
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days*24*60*60*1000));
            expires = "; expires=" + date.toUTCString();
        }
        document.cookie = name + "=" + (value || "")  + expires + "; path=/";
    }
    function getCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }
    
    //get params
    const url_string = window.location.href;
    const url_object = new URL(url_string);
    const my_param = url_object.searchParams.get("my_param");
    
    if (my_param != null) {
        
        //set cookie
        setCookie('my_param', my_param);
    
    }else{
    
        //get cookie
        let my_param_cookie = getCookie('my_param');
    
        if (my_param_cookie) {
    
            //set params from cookie
    
            let url_all_params = '';
            const url_pathname = url_object.pathname;
            const url_params = url_object.search;
            const url_seperator = url_params == '' ? '?' : '&';
            const url_my_param = my_param == null ? 'my_param=' + my_param_cookie : my_param;
    
            url_all_params = url_pathname + url_params + url_seperator + url_my_param;
    
            if (history.pushState) {
                window.history.pushState({}, "", url_all_params);
            } else {
                document.location.href = url_all_params;
            }
        }
    }
    

    To the code above:

    1. Find the parameter you are looking for
    2. When you find it, save it in a cookie
    3. If you haven't found it, see if the cookie exists
    4. If the cookie exists, get the parameter you are looking for and add it to the URL
    5. If the cookie doesn't exist, do nothing

    This will make the referring_site look like this in the order webhook:

    "referring_site": https://{shop}.myshopify.com/products/testprodukt-1/?myparam=1

    Now you can do what you want with the parameter in the backend, I hope I could help or provide inspiration.


  2. There is a field called landing_site on the Shopify order object you can access via the api. That should give you the data you’re after.

    You can also track UTM codes in Shopify’s built in reporting, so you may be able to leverage that without the need for an additional service.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search