cloudinary-community / gatsby-transformer-cloudinary

Use Cloudinary images with gatsby-image for high performance and total control!
https://gatsby-transformer-cloudinary.netlify.app/
MIT License
69 stars 28 forks source link

Add custom analytics code for generated Cloudinary URLs #229

Closed colbyfayock closed 1 year ago

colbyfayock commented 1 year ago

To give the ability to track Gatsby usage in addition to or separate from the standard JS usage, we'd like to add a custom Analytics ID

The Analytics token is appended at the end of Cloudinary URLs as a query parameter by the SDKs, giving us the ability to have insights as to what SDKs or tools are making Cloudinary requests.

The relevant code depends on the SDK being used, but the Cloudinary URL Gen SDK would be added similar to this work from Next Cloudinary:

https://github.com/colbyfayock/next-cloudinary/blob/ab7ba447af6eff264d9baf984ca513b5910a316e/next-cloudinary/src/lib/cloudinary.ts#L101

To implement, set the real versions including:

raae commented 1 year ago

It seems to me that the NodeJS SDK url generation does not support tracking. For Gatsby these urls are created in the resolver when the site is built (aka. in a node environment).

Will it be added for feature parity with the Cloudinary URL-Gen SDK?

colbyfayock commented 1 year ago

did a little digging and it seems like it might be available?

https://github.com/cloudinary/cloudinary_npm/blob/ef6f5ad9eb1c0834c5796b833b4a7247a88cd39a/lib/utils/index.js#L833

have you tried passing the options directly into the url method? though the object.assign would make me think it would get overridden.

if that doesnt work ill ask the sdk team about seeing if we can get it added

raae commented 1 year ago

Setting urlAnalytics to true added ?_s=AMAzlWK0 to the end of the URL.

  const url = cloudinary.url(publicId, {
    cloud_name: cloudName,
    secure: options.secure,
    transformation: transformation,
    flags: flags,
    urlAnalytics: true,
  });

But I was not able to change the code added:

const url = cloudinary.url(publicId, {
    cloud_name: cloudName,
    secure: options.secure,
    transformation: transformation,
    flags: flags,
    urlAnalytics: true,
    sdkCode: 'X',
  });

Looking at the code, I think you are right in that even if the sdkCode for instance gets passed along it will be overridden. The code creating the sdkVersions object used last in that assign you pointed to always returns an object with sdkCode, sdkSemver and techVersionset.

colbyfayock commented 1 year ago

@raae https://github.com/cloudinary/cloudinary_npm/issues/590

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 4.4.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: