Hi! I have a problem with a custom transformation ...
# support
e
Hi! I have a problem with a custom transformation which i made.
import { UAParser } from "@rs/userAgentParser/v1"; async function getGeolocation(event) { if (event.request_ip) { const url = `https://api.findip.net/${event.request_ip}/?token=****************************`; // The FindIP API endpoint const res = await fetch(url); if (res.ok) { const data = await res.json(); // Parse the response data as JSON return data; // Return the data } } return null; } function getUserAgentInfo(event) { const userAgent = event.context?.userAgent; if (userAgent) { const parser = new UAParser(); const parsedUserAgent = parser.setUA(userAgent).getResult(); return parsedUserAgent; } return null; } export async function transformEvent(event, metadata) { const geolocationPromise = getGeolocation(event); const userAgentInfo = getUserAgentInfo(event); if (userAgentInfo) { event.context.parsedUserAgent = userAgentInfo; } const geolocationData = await geolocationPromise; if (geolocationData) { event.context.geolocation = geolocationData; } return event; } It should do 'parse user agent' and 'geolocation' transformations. The ip api url is good, I have tested it. But, it doesn't do geolocation transformation on my data
h
Hi @eager-terabyte-34579 Can you please try with the
fetchV2
API as indicated in the transformation templates here?
e
Here is the updated code for the transformation. It does the 'parse user agent' part, but I don't get any geolocation-related columns in my data lake. import { UAParser } from "@rs/userAgentParser/v1"; async function getGeolocation(event) { if (event.request_ip) { const url = `https://api.findip.net/${event.request_ip}/?token=*************************`; // The FindIP API endpoint const res = await fetchV2(url); if (res.ok) { const data = await res.json(); // Parse the response data as JSON return data; // Return the data } } return null; } function getUserAgentInfo(event) { const userAgent = event.context?.userAgent; if (userAgent) { const parser = new UAParser(); const parsedUserAgent = parser.setUA(userAgent).getResult(); return parsedUserAgent; } return null; } export async function transformEvent(event, metadata) { const geolocationPromise = getGeolocation(event); const userAgentInfo = getUserAgentInfo(event); if (userAgentInfo) { event.context.parsedUserAgent = userAgentInfo; } const geolocationData = await geolocationPromise; if (geolocationData) { event.context.geolocation = geolocationData; } return event; }
h
Have you tested the transformation with some sample data in the transformations view?
e
Here is the result of the test. This is payload (page event): [ { "anonymousId": "8d872292709c6fbe", "channel": "mobile", "context": { "app": { "build": "1", "name": "AMTestProject", "namespace": "com.rudderstack.android.rudderstack.sampleAndroidApp", "version": "1.0" }, "device": { "id": "8d872292709c6fbe", "manufacturer": "Google", "model": "AOSP on IA Emulator", "name": "generic_x86_arm", "type": "android" }, "library": { "name": "com.rudderstack.android.sdk.core", "version": "1.0.2" }, "locale": "en-US", "network": { "carrier": "Android", "bluetooth": false, "cellular": true, "wifi": true }, "os": { "name": "Android", "version": "9" }, "screen": { "density": 420, "height": 1794, "width": 1080 }, "timezone": "Asia/Kolkata", "traits": { "address": { "city": "Kolkata", "country": "India", "postalcode": "700096", "state": "West bengal", "street": "Park Street" }, "age": "30", "anonymousId": "8d872292709c6fbe", "birthday": "2020-05-26", "createdat": "18th March 2020", "description": "Premium User for 3 years", "email": "page@test.com", "firstname": "John", "userId": "sample_user_id", "lastname": "Sparrow", "name": "John Sparrow", "id": "sample_user_id", "phone": "9876543210", "username": "john_sparrow", "quantity": "5", "price": "56.0" }, "userAgent": "Dalvik/2.1.0 (Linux; U; Android 9; AOSP on IA Emulator Build/PSR1.180720.117)" }, "event": "page", "integrations": { "All": true }, "messageId": "1590431830865-3be680d6-7dcd-4b05-8460-f3acc30046d9", "originalTimestamp": "2020-05-25T183710.865Z", "type": "page", "userId": "sample_user_id" } ] And here is the output: [ { "anonymousId": "8d872292709c6fbe", "channel": "mobile", "context": { "app": { "build": "1", "name": "AMTestProject", "namespace": "com.rudderstack.android.rudderstack.sampleAndroidApp", "version": "1.0" }, "device": { "id": "8d872292709c6fbe", "manufacturer": "Google", "model": "AOSP on IA Emulator", "name": "generic_x86_arm", "type": "android" }, "library": { "name": "com.rudderstack.android.sdk.core", "version": "1.0.2" }, "locale": "en-US", "network": { "carrier": "Android", "bluetooth": false, "cellular": true, "wifi": true }, "os": { "name": "Android", "version": "9" }, "screen": { "density": 420, "height": 1794, "width": 1080 }, "timezone": "Asia/Kolkata", "traits": { "address": { "city": "Kolkata", "country": "India", "postalcode": "700096", "state": "West bengal", "street": "Park Street" }, "age": "30", "anonymousId": "8d872292709c6fbe", "birthday": "2020-05-26", "createdat": "18th March 2020", "description": "Premium User for 3 years", "email": "page@test.com", "firstname": "John", "userId": "sample_user_id", "lastname": "Sparrow", "name": "John Sparrow", "id": "sample_user_id", "phone": "9876543210", "username": "john_sparrow", "quantity": "5", "price": "56.0" }, "userAgent": "Dalvik/2.1.0 (Linux; U; Android 9; AOSP on IA Emulator Build/PSR1.180720.117)", "parsedUserAgent": { "ua": "Dalvik/2.1.0 (Linux; U; Android 9; AOSP on IA Emulator Build/PSR1.180720.117)", "browser": {}, "engine": {}, "os": { "name": "Android", "version": "9" }, "device": { "vendor": "Generic", "model": "Android 9" }, "cpu": {} } }, "event": "page", "integrations": { "All": true }, "messageId": "1590431830865-3be680d6-7dcd-4b05-8460-f3acc30046d9", "originalTimestamp": "2020-05-25T183710.865Z", "type": "page", "userId": "sample_user_id" } ]
h
Hi @mammoth-answer-4269, you have to update your function (as suggested in the documentation example) to make it work:
Copy code
async function getGeolocation(event) {
    if (event.request_ip) {
        const url = `https://api.findip.net/${event.request_ip}/?token=*************************`; // The FindIP API endpoint
        const res = await fetchV2(url);
        return res.body;
    }
    return null;
}
For testing the event in the transformation UI, you can add the field
request_ip
to the input event object. In the actual ingestion pipeline, the server automatically populates this field.