Skip to main content

How do I add a tracking link to my email templates?

In this article, we'll show you how to adjust your email templates so customers receive the right tracking link with the shipping confirmation. We primarily assume that you use a separate shipping method for each carrier. If you only have one shipping method with multiple carriers, you'll find a dedicated section on that further below.

Setting up tracking links

First, enter the respective tracking URL for your shipping methods under Settings → Shipping. Use %s as the placeholder for the actual tracking code.

Sendungsverfolgung_Link1.png

Here's an overview of the URLs for each carrier:

Adjusting the email template

Next, we'll add the tracking URL to the email template. Go to Settings → Shop → Email Templates and search for the template Delivery state enters status: Shipped. Click Edit.

Now scroll to the Mail text section and enter the following code in the Text and HTML fields at the position where the tracking link should appear. When you create a shipping label, the generated tracking number is automatically saved in the order details under Details → Shipping → Tracking number(s). This variable, trackingCode, is then inserted into the email.

In the Text field:

{% for delivery in order.deliveries %}
{% for trackingCode in delivery.trackingCodes %}
{% set trackingLink = delivery.shippingMethod.trackingUrl|replace({'%s': trackingCode}) %}
Sendung verfolgen ({{ trackingCode }}): {{ trackingLink }}
{% endfor %}
{% endfor %}

In the HTML field:

{% for delivery in order.deliveries %}
{% for trackingCode in delivery.trackingCodes %}
{% set trackingLink = delivery.shippingMethod.trackingUrl|replace({'%s': trackingCode}) %}
<p><a href="{{ trackingLink }}">Sendung verfolgen ({{ trackingCode }})</a></p>
{% endfor %}
{% endfor %}

Checking flows

If you add another carrier and use custom shipping flows, check whether it needs to be added there as well.

Special case: multiple carriers but only one shipping method

Use this method if you only offer one shipping method at checkout (e.g. "Standard shipping"), but decide flexibly in the warehouse which carrier to ship with. In this case, the tracking URL is not stored in the shipping method but directly in the email template. The same URLs as above generally apply here too:

In our example, we ship either with DHL or DPD. In the email, the appropriate link is then chosen based on the length of the stored tracking code. The basic assumption here is that DHL tracking codes are usually longer than 14 characters, while DPD tracking codes are shorter.

Go to Settings → Shop → Email Templates and open the template Delivery state enters status: Shipped. In the Mail text section, enter the following code at the position where the tracking link should appear:

In the Text field

{% for delivery in order.deliveries %}
{% for trackingCode in delivery.trackingCodes %}
{% if trackingCode|length > 14 %}
{% set trackingLink = 'https://www.dhl.de/de/privatkunden/pakete-empfangen/verfolgen.html?piececode=' ~ trackingCode %}
DHL Sendungsverfolgung: {{ trackingLink }}
{% else %}
{% set trackingLink = 'https://tracking.dpd.de/status/de_DE/parcel/' ~ trackingCode %}
DPD Sendungsverfolgung: {{ trackingLink }}
{% endif %}
{% endfor %}
{% endfor %}

In the HTML field

{% for delivery in order.deliveries %}
{% for trackingCode in delivery.trackingCodes %}
<p>
{% if trackingCode|length > 14 %}
{% set trackingLink = 'https://www.dhl.de/de/privatkunden/pakete-empfangen/verfolgen.html?piececode=' ~ trackingCode %}
DHL Sendungsverfolgung: <a href="{{ trackingLink }}">{{ trackingCode }}</a>
{% else %}
{% set trackingLink = 'https://tracking.dpd.de/status/de_DE/parcel/' ~ trackingCode %}
DPD Sendungsverfolgung: <a href="{{ trackingLink }}">{{ trackingCode }}</a>
{% endif %}
</p>
{% endfor %}
{% endfor %}

Special case DSV

With DSV, there's a special case: the tracking number is not carried over 1:1 into the tracking URL, but is instead converted into an ID via API. As an alternative, you could link directly to the tracking website (https://mydsv.com/new/tracking/track-shipment), where the tracking number then has to be entered manually.

Here's an example where the variable delivery.shippingMethod.translated.name is first used to check whether the shipping method's name contains 'DSV'. If so, the link to DSV's general tracking page is inserted; otherwise, the stored tracking link of the respective shipping method is used.

In the Text field

{% for delivery in order.deliveries %}
{% for trackingCode in delivery.trackingCodes %}
{% if "DSV" in delivery.shippingMethod.translated.name %}
Dein Paket wurde per DSV versendet.
Deine Sendungsnummer lautet: {{ trackingCode }}
Du kannst den Status deiner Sendung hier abrufen:
https://mydsv.com/new/tracking/track-shipment
{% else %}
{% set trackingLink = delivery.shippingMethod.trackingUrl|replace({'%s': trackingCode}) %}
Du kannst den Status deiner Sendung hier abrufen: {{ trackingLink }}
{% endif %}
{% endfor %}
{% endfor %}

In the HTML field

{% for delivery in order.deliveries %}
{% for trackingCode in delivery.trackingCodes %}
{% if "DSV" in delivery.shippingMethod.translated.name %}
<p>Dein Paket wurde per <strong>DSV</strong> versendet.</p>
<p>Deine Sendungsnummer lautet: <strong>{{ trackingCode }}</strong></p>
<p>Du kannst den Status deiner Sendung hier abrufen:<br>
<a href="https://mydsv.com/new/tracking/track-shipment">DSV Sendungsverfolgung öffnen</a>
</p>
{% else %}
{% set trackingLink = delivery.shippingMethod.trackingUrl|replace({'%s': trackingCode}) %}
<p>Du kannst den Status deiner Sendung hier abrufen: <a href="{{ trackingLink }}">{{ trackingCode }}</a></p>
{% endif %}
{% endfor %}
{% endfor %}
Did this answer your question?