ash-jc-allen / short-url

A Laravel package for creating shortened URLs for your web apps.
MIT License
1.25k stars 157 forks source link

Add UTM tracking #224

Closed afiqiqmal closed 6 months ago

afiqiqmal commented 10 months ago

This PR is to add an option to record UTM parameters.

Detect UTM from Short Link


$this->get('/short/12345?utm_source=newsletter&utm_medium=email&utm_campaign=spring_sale&utm_content=promo_banner')
            ->assertStatus(301)
            ->assertRedirect('https://domain.com');

$this->assertDatabaseHas('short_url_visits', [
    'short_url_id' => $shortURL->id,
    'utm_source'   => 'newsletter',
    'utm_medium'   => 'email',
    'utm_campaign' => 'spring_sale',
    'utm_content'  => 'promo_banner',
]);

Detect UTM within Short Link

$this->get('/short/12345')
            ->assertStatus(301)
            ->assertRedirect('https://domain.com?utm_source=newsletter&utm_medium=email&utm_campaign=spring_sale&utm_content=promo_banner');

$this->assertDatabaseHas('short_url_visits', [
    'short_url_id' => $shortURL->id,
    'utm_source'   => 'newsletter',
    'utm_medium'   => 'email',
    'utm_campaign' => 'spring_sale',
    'utm_content'  => 'promo_banner',
]);

UTM fallback if within destination url doesn't include UTM param.

if ($shortURL->track_utm) {

    $parsedUrl = parse_url($shortURL->destination_url);

    if (isset($parsedUrl['query'])) {
        parse_str($parsedUrl['query'], $parsedQuery);
    }

    $visit->utm_source = $parsedQuery['utm_source'] ?? $request->query('utm_source');
    $visit->utm_medium = $parsedQuery['utm_medium'] ?? $request->query('utm_medium');
    $visit->utm_campaign = $parsedQuery['utm_campaign'] ?? $request->query('utm_campaign');
    $visit->utm_term = $parsedQuery['utm_term'] ?? $request->query('utm_term');
    $visit->utm_content = $parsedQuery['utm_content'] ?? $request->query('utm_content');
}
ash-jc-allen commented 10 months ago

Hey @afiqiqmal, thanks for such a thorough PR! I'm pretty snowed under with work at the moment, but I'll try and take a proper look at this ASAP. I'm also not up to speed with UTM tracking, so I'll probably need to do a bit of reading up too before I can review this.

As a side note, this PR has made me wonder whether it might be better long term to have a query_params field in the short_url_visits table that records all the query params in a JSON object? I feel like that'd open the possibility for recording all query params rather than just specific ones like UTM. Do you have any thoughts on this? 🙂

afiqiqmal commented 10 months ago

@ash-jc-allen i also have thought of that merging into one column with json data type. But it will come pros and cons which the cons is indexing concern. So i decide to do the specific for UTM for easier and faster query for analytics . If other than that UTM, i think it would be great to put in query_params column.

ash-jc-allen commented 6 months ago

Hey @afiqiqmal! I'm really sorry for taking so long to get back to you on this one. Personal life got (and still is) really busy, so my open-source efforts haven't been great!

I think because I'm unsure of the best way to tackle this, I'm probably going to hold off merging this PR. But if enough people want this feature adding, I'll definitely revisit it.

Thanks again for the PR, I really appreciate it! 😄